Reputation: 49
Im trying to revize an old basic shopping app with firebase but its old version code and couldnt get the null safety exactly. Could anyone help me. Here i get "non-nullable instance field '_selectedParam' must be initialized" also for 'database' i get the same error. For 'List's i get "The default 'List' constructor isn't available when null safety is enabled." error.
class MyApp extends StatefulWidget {
MyApp(this.k);
final int k;
@override
_MyAppState createState() => _MyAppState(k);
}
class _MyAppState extends State<MyApp> {
CollectionReference games= FirebaseFirestore.instance.collection("games");
_MyAppState(this.k);
final int k ;
static Future<Database> database ;
var receivePort = ReceivePort();
Isolate _isolate;
final String url = "https://www.freetogame.com/api/games";
List data ;
static List listpvp= new List(); static List listmmorpg= new List(); static List listmmofps= new List(); static List listshooter = new List();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
String _selectedParam;
int val;
String task;
static String _col="mmofps"; //initially
Upvotes: 2
Views: 157
Reputation: 1242
The main thing there is to understand about null safety, is that flutter/dart should be able to know exactly wether a variable is defined or not at any point in a widget’s lifecycle. In your example, _selectParam is typed as a String, so dart expects it to always be of type string, and to be defined. But if you declare it as you did, it’s going to stay null until you assign a string to it. So there are 2 options basically:
String? _selectParam
late String _selectParam
As for the second error, you cannot use the default List() constructor with null safety anymore, it is deprecated. You can just use the other List constructors. List.generate() or List.of() for example.
In your case, you could do:
List listmmorpg = List.of([])
Please refer to the official Dart documentation to see which constructors suits you the most.
Upvotes: 2
Reputation: 316
class MyApp extends StatefulWidget {
MyApp(this.k);
int? k;
@override
_MyAppState createState() => _MyAppState(k);
}
class _MyAppState extends State<MyApp> {
CollectionReference games= FirebaseFirestore.instance.collection("games");
_MyAppState(this.k);
int? k ;
static Future<Database> database ;
var receivePort = ReceivePort();
Isolate? _isolate;
String? url = "https://www.freetogame.com/api/games";
List? data ;
static List listpvp= new List(); static List listmmorpg= new List(); static List listmmofps= new List(); static List listshooter = new List();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
String? _selectedParam;
int? val;
String? task;
static String _col="mmofps"; //initially
Upvotes: 1