when I upgrade flutter to 2.15, errors disppay as following:
import 'package:shared_preferences/shared_preferences.dart';
class SharedPreferencesUtil {
SharedPreferencesUtil._();
static SharedPreferencesUtil _instance;
SharedPreferences sharedPreferences;
static SharedPreferencesUtil getInstance() {
if (_instance == null) {
_instance = SharedPreferencesUtil._();
}
return _instance;
}
}
Error:
Non-nullable instance field '_instance' must be initialized.
Non-nullable instance field 'sharedPreferences' must be initialized.
The non-nullable variable '_instance' must be initialized.
shared_preferences: ^0.5.7+3
I don't know how to fix it
Because you define sharedPreferences variable but you not assign it with a value so the error appear. Try to fix as below:
class SharedPreferencesUtil {
SharedPreferencesUtil._();
static SharedPreferencesUtil? _instance;
SharedPreferences? sharedPreferences;
static Future<SharedPreferencesUtil?> getInstance() {
if (_instance == null) {
var singleton = SpUtil._();
await singleton._init();
_instance = singleton;
}
return _instance;
}
}
Future _init() async {
sharedPreferences = await SharedPreferences.getInstance();
}
Related
I'm trying to implement an event callback directly in the constructor, but for some reason it does not compile and I do not understand what's the issue with my code.
abstract class Base {
final Future<bool>? onMagic;
Base({
this.onMagic
});
Future<void> doSomething() async {
if(onMagic != null) {
// does not work... why?
// final useMagic = await onMagic!();
onMagic?.then((magic) {
if(magic) print("TODO make something magical");
});
}
}
}
class RealMagic extends Base {
RealMagic() : super(
// Error: The argument type 'Future<bool> Function()' can't be assigned to the parameter type 'Future<bool>?'.
onMagic: () async => await _magic();
);
Future<bool> _magic() async {
return true;
}
}
I inlined the error above. If that's not possible which alternatives do I have to handle the optional callback?
The problem is with the type of the onMagic. It's not a Future<bool>, it should be a Future<bool> Function()?
abstract class Base {
final Future<bool> Function()? onMagic;
Base({this.onMagic});
Future<void> doSomething() async {
onMagic?.call().then((magic) {
if (magic) print("TODO make something magical");
});
}
}
class RealMagic extends Base {
RealMagic()
: super(
onMagic: () async => await _magic(),
);
static Future<bool> _magic() async { // Made this static so it can be accessed in the constructor
return true;
}
}
I would like to pass some initial information into a singleton in dart.
Unfortunately, the information I like to access is null (see dartpad output below)
It seems like I get a new instance of my object and not the singleton but I can not wrap my head around it. Any idea?
ElmCommandProvider.fromMetaData
ElmCommandProvider._internal()
ElmCommandProvider._init
ElmCommandProvider()
null
This is the code which can be pasted in DartPad
class Command {
Command(this.i);
final int i;
}
class MetaData {
MetaData(this.i);
final int i;
}
class ElmCommandProvider {
List<Command> commandsList;
bool _lock = false;
static Map<String, MetaData> _metaDataPool;
factory ElmCommandProvider.fromMetaData(Map<String, MetaData> metaDataPool) {
print('ElmCommandProvider.fromMetaData');
assert(!_singleton._lock, "it's a singleton that can't re-defined");
ElmCommandProvider._metaDataPool = metaDataPool;
_singleton._lock = true;
ElmCommandProvider._init();
return _singleton;
}
factory ElmCommandProvider() {
print('ElmCommandProvider()');
return _singleton;
}
static final ElmCommandProvider _singleton =
new ElmCommandProvider._internal();
ElmCommandProvider._internal() {
print('ElmCommandProvider._internal()');
}
ElmCommandProvider._init() {
print('ElmCommandProvider._init');
commandsList =
_metaDataPool.values.map((bloc) => Command(bloc.i)).toList();
}
}
void main() {
ElmCommandProvider.fromMetaData({'1': MetaData(1), '2': MetaData(2)});
print( ElmCommandProvider().commandsList);
}
_init() should not be a constructor. Or at least there is no need for it to be one and it's confusing you. It should be changed to a static method or a private instance method.
When you do commandsList= in ElmCommandProvider._init(), commandsList is referring to the commandsList instance variable in the new ElmCommandProvider object you're creating with the constructor. You likely actually mean to modify the singleton's commandsList so you should have been doing singleton.commandsList = instead of just commandsList =.
Example working code with static method:
class Command {
Command(this.i);
final int i;
}
class MetaData {
MetaData(this.i);
final int i;
}
class ElmCommandProvider {
List<Command> commandsList;
bool _lock = false;
static Map<String, MetaData> _metaDataPool;
factory ElmCommandProvider.fromMetaData(Map<String, MetaData> metaDataPool) {
print('ElmCommandProvider.fromMetaData');
assert(!_singleton._lock, "it's a singleton that can't re-defined");
ElmCommandProvider._metaDataPool = metaDataPool;
_singleton._lock = true;
_init();
return _singleton;
}
factory ElmCommandProvider() {
print('ElmCommandProvider()');
return _singleton;
}
static final ElmCommandProvider _singleton =
new ElmCommandProvider._internal();
ElmCommandProvider._internal() {
print('ElmCommandProvider._internal()');
}
static _init() {
print('ElmCommandProvider._init');
_singleton.commandsList =
_metaDataPool.values.map((bloc) => Command(bloc.i)).toList();
}
}
void main() {
ElmCommandProvider.fromMetaData({'1': MetaData(1), '2': MetaData(2)});
print( ElmCommandProvider().commandsList);
}
Example working code with private instance method:
class Command {
Command(this.i);
final int i;
}
class MetaData {
MetaData(this.i);
final int i;
}
class ElmCommandProvider {
List<Command> commandsList;
bool _lock = false;
static Map<String, MetaData> _metaDataPool;
factory ElmCommandProvider.fromMetaData(Map<String, MetaData> metaDataPool) {
print('ElmCommandProvider.fromMetaData');
assert(!_singleton._lock, "it's a singleton that can't re-defined");
ElmCommandProvider._metaDataPool = metaDataPool;
_singleton._lock = true;
_singleton._init();
return _singleton;
}
factory ElmCommandProvider() {
print('ElmCommandProvider()');
return _singleton;
}
static final ElmCommandProvider _singleton =
new ElmCommandProvider._internal();
ElmCommandProvider._internal() {
print('ElmCommandProvider._internal()');
}
void _init() {
print('ElmCommandProvider._init');
commandsList =
_metaDataPool.values.map((bloc) => Command(bloc.i)).toList();
}
}
void main() {
ElmCommandProvider.fromMetaData({'1': MetaData(1), '2': MetaData(2)});
print( ElmCommandProvider().commandsList);
}
I was going through some code when I found this snippet:
class DBHelper {
DBHelper._();
static final DBHelper db = DBHelper._();
static Database _database;
Future<Database> get database async {
if (_database == null) {
_database = await initDB();
}
return _database;
}
}
Can anyone simplify this definition of Named Constructor?
Also can there be any alternative way of achieving this functionality.
I want to use pusher sdk in Flutter from android native code because its library no yet completely supported in flutter but when i send first message it received it successfully the next message make app crush with Reply already submitted error her on this line result.success(txt);
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "demo.gawkat.com/info";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler((methodCall, result) -> {
final Map<String, Object> arguments = methodCall.arguments();
if (methodCall.method.equals("getMessage")) {
Pusher pusher = new Pusher("faa685e4bb3003eb825c");
pusher.connect();
Channel channel = pusher.subscribe("messages");
channel.bind("new_message", (channelName, eventName, data) -> runOnUiThread(() -> {
Gson gson = new Gson();
Message message = gson.fromJson(data, Message.class);
String txt = message.text;
result.success(txt);
}));
}
});
}
}
Flutter code:
Future<String> _getMessage() async {
String value;
try {
value = await platform.invokeMethod('getMessage');
} catch (e) {
print(e);
}
return value;
}
Error is
FATAL EXCEPTION: main
Process: com.example.flutter_app, PID: 6296
java.lang.IllegalStateException: Reply already submitted
at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl$1.reply(FlutterNativeView.java:197)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:204)
at com.example.flutter_app.MainActivity.lambda$null$0(MainActivity.java:40)
at com.example.flutter_app.-$$Lambda$MainActivity$axbDTe2B0rhavWD22s4E8-fuCaQ.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767
I think it is happening after Flutter upgrade > 1.5.4.hotfix.
Anyway, Yes there is a solution (Refer this github issue),
In your Activitybelow onCreate() add this class:
private static class MethodResultWrapper implements MethodChannel.Result {
private MethodChannel.Result methodResult;
private Handler handler;
MethodResultWrapper(MethodChannel.Result result) {
methodResult = result;
handler = new Handler(Looper.getMainLooper());
}
#Override
public void success(final Object result) {
handler.post(
new Runnable() {
#Override
public void run() {
methodResult.success(result);
}
});
}
#Override
public void error(
final String errorCode, final String errorMessage, final Object errorDetails) {
handler.post(
new Runnable() {
#Override
public void run() {
methodResult.error(errorCode, errorMessage, errorDetails);
}
});
}
#Override
public void notImplemented() {
handler.post(
new Runnable() {
#Override
public void run() {
methodResult.notImplemented();
}
});
}
}
Then, instead of using MethodChannel result to setMethodCallHandler argument callback add name as rawResult and then inside that callback, add this line:
MethodChannel.Result result = new MethodResultWrapper(rawResult);
As below:
//......
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
(call, rawResult) -> {
MethodChannel.Result result = new MethodResultWrapper(rawResult);
//.....
I use flags for this problem.
Just make sure that methods of same channels are called simultaneously.
The problem seem to appear then.
If two methods needs to be called simulatenously without any problem define both methods in 2 different channels
var resultMap = Map<String, MethodChannel.Result> = HashMap()
new MethodChannel(getFlutterView(), CHANNEL_1).setMethodCallHandler((methodCall, result) -> {
final Map<String, Object> arguments = methodCall.arguments();
if (methodCall.method.equals("method1")) {
// implement method 1
}
});
new MethodChannel(getFlutterView(), CHANNEL_2).setMethodCallHandler((methodCall, result) -> {
final Map<String, Object> arguments = methodCall.arguments();
if (methodCall.method.equals("method2")) {
resultMap = resultMap + mapOf(CHANNEL_2 to MethodResultWrapper(result) // use this later to return result
// implement method2
result.success(true) // or whatever value
}
});
This reduce the chance of "Reply already submitted" error.
Incase if you are using MethodResultWrapper as #Blasanka answer use flags before result.success
when method is invoked set flag to true
val methodCheckFlag: Boolean = true
then when result need to be returned
if(methodCheckFlag) {
methodCheckFlag = false;
methodWrapperResult?.success(true) // or what ever value to return
}
or use the saved MethodResultWrapper as
if(methodCheckFlag) {
methodCheckFlag = false;
resultMap[CHANNEL_2]?.success(true) // or what ever value to return
}
#FacesValidator("uniqueValidator")
public class UniqueValidator implements Validator
{
#Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
{
if(context == null)
{
throw new NullPointerException();
}
if(component == null)
{
throw new NullPointerException();
}
ValueExpression valueExpression = component.getValueExpression("value");
if(valueExpression == null)
{
return;
}
ValueReference valueReference = valueExpression.getValueReference(context.getELContext());
if(valueReference == null)
{
return;
}
Object base = valueReference.getBase();
Object property = valueReference.getProperty();
Tracer.out(base, property);
.....
}
}
and ValueReference is always null.
what's wrong?
thank you BeanValidator :)
public class ValueExpressionAnalyzer
{
public static ValueReference getReference(ELContext elContext, ValueExpression expression)
{
InterceptingResolver resolver = new InterceptingResolver(elContext.getELResolver());
try
{
expression.setValue(new InterceptingContext(elContext, resolver), null);
}
catch(ELException ele)
{
return null;
}
ValueReference reference = resolver.getValueReference();
if(reference != null)
{
Object base = reference.getBase();
if(base instanceof CompositeComponentExpressionHolder)
{
ValueExpression ve = ((CompositeComponentExpressionHolder) base).getExpression((String) reference.getProperty());
if(ve != null)
{
reference = getReference(elContext, ve);
}
}
}
return reference;
}
private static class InterceptingContext extends ELContext
{
private final ELContext context;
private final ELResolver resolver;
public InterceptingContext(ELContext context, ELResolver resolver)
{
this.context = context;
this.resolver = resolver;
}
// punch in our new ELResolver
#Override
public ELResolver getELResolver()
{
return resolver;
}
// The rest of the methods simply delegate to the existing context
#Override
public Object getContext(Class key)
{
return context.getContext(key);
}
#Override
public Locale getLocale()
{
return context.getLocale();
}
#Override
public boolean isPropertyResolved()
{
return context.isPropertyResolved();
}
#Override
public void putContext(Class key, Object contextObject)
{
context.putContext(key, contextObject);
}
#Override
public void setLocale(Locale locale)
{
context.setLocale(locale);
}
#Override
public void setPropertyResolved(boolean resolved)
{
context.setPropertyResolved(resolved);
}
#Override
public FunctionMapper getFunctionMapper()
{
return context.getFunctionMapper();
}
#Override
public VariableMapper getVariableMapper()
{
return context.getVariableMapper();
}
}
private static class InterceptingResolver extends ELResolver
{
private final ELResolver delegate;
private ValueReference valueReference;
public InterceptingResolver(ELResolver delegate)
{
this.delegate = delegate;
}
public ValueReference getValueReference()
{
return valueReference;
}
// Capture the base and property rather than write the value
#Override
public void setValue(ELContext context, Object base, Object property, Object value)
{
if(base != null && property != null)
{
context.setPropertyResolved(true);
valueReference = new ValueReference(base, property.toString());
}
}
// The rest of the methods simply delegate to the existing context
#Override
public Object getValue(ELContext context, Object base, Object property)
{
return delegate.getValue(context, base, property);
}
#Override
public Class<?> getType(ELContext context, Object base, Object property)
{
return delegate.getType(context, base, property);
}
#Override
public boolean isReadOnly(ELContext context, Object base, Object property)
{
return delegate.isReadOnly(context, base, property);
}
#Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
{
return delegate.getFeatureDescriptors(context, base);
}
#Override
public Class<?> getCommonPropertyType(ELContext context, Object base)
{
return delegate.getCommonPropertyType(context, base);
}
}
}
Have a look at Omnifaces ExpressionInspector. Seems in JSF 2.x its not implemented and always returns null.
https://showcase.omnifaces.org/el/ExpressionInspector