Reputation: 198
I was trying to build a flutter quiz maker app and I run into some errors. The errors I got while implementing were as following:
Error 1: Non-nullable instance field '_isLoading' must be initialized.
Error 2: A value of type 'Set<String?>' can't be assigned to a variable of type 'Map<String, String>'.
Here is the Code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:quizmaker/services/database.dart';
import 'package:quizmaker/widgets/widgets.dart';
class AddQuestion extends StatefulWidget {
final String? quizId;
AddQuestion(this.quizId);
@override
_AddQuestionState createState() => _AddQuestionState();
}
class _AddQuestionState extends State<AddQuestion> {
final _formKey = GlobalKey<FormState>();
String? question, option1, option2, option3, option4;
bool _isLoading;
DatabaseServices databaseService = new DatabaseServices();
uploadQuizData(){
if(_formKey.currentState!.validate()){
Map<String, String> questionMap = {
"question" = question,
"option1" = option1,
"option2" = option2,
"option3" = option3,
"option4" = option4,
};
databaseService.addQuestionData(questionMap, widget.quizId.toString());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: appBar(context),
backgroundColor: Colors.transparent,
elevation: 0,
iconTheme: IconThemeData(color: Colors.black87),
brightness: Brightness.light,
),
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 24.0,
),
child: _isLoading ? Container(
child: Center(
child: CircularProgressIndicator(),
),
) : Container(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Column(
children: [
TextFormField(
validator: (value) => value!.isEmpty ? "Enter Question" : null,
decoration: InputDecoration(
hintText: "Question",
),
onChanged: (value) {
question = value;
},
),
SizedBox(
height: 6.0,
),
TextFormField(
validator: (value) => value!.isEmpty ? "Enter Option1" : null,
decoration: InputDecoration(
hintText: "Option 1 (Correct Answer)",
),
onChanged: (value) {
option1 = value;
},
),
SizedBox(
height: 6.0,
),
TextFormField(
validator: (value) => value!.isEmpty ? "Enter Option2" : null,
decoration: InputDecoration(
hintText: "Option 2",
),
onChanged: (value) {
option2 = value;
},
),
SizedBox(
height: 6.0,
),
TextFormField(
validator: (value) => value!.isEmpty ? "Enter Option3" : null,
decoration: InputDecoration(
hintText: "Option 3",
),
onChanged: (value) {
option3 = value;
},
),
SizedBox(
height: 6.0,
),
TextFormField(
validator: (value) => value!.isEmpty ? "Enter Option4" : null,
decoration: InputDecoration(
hintText: "Option 4",
),
onChanged: (value) {
option4 = value;
},
),
Spacer(),
Row(
children: [
blueButton(
context: context,
label: "Submit",
buttonWidth: MediaQuery.of(context).size.width / 2 - 36,
),
SizedBox(
width: 24.0,
),
GestureDetector(
onTap: (){
setState(() {
});
},
child: blueButton(
context: context,
label: "Add Question",
buttonWidth: MediaQuery.of(context).size.width / 2 - 36,
),
),
SizedBox(
height: 60.0,
),
],
),
],
),
),
),
);
}
}
Upvotes: 1
Views: 957
Reputation: 499
Try replacing the questionMap
with:
Map<String, String> questionMap = {
"question" : question,
"option1" : option1,
"option2" : option2,
"option3" : option3,
"option4" : option4,
};
You also need to give a default value for your _isLoading
variable.
Replacing it with bool _isLoading = false;
should work.
Upvotes: 2
Reputation: 12373
Change it to :
not =
Map<String, String> questionMap = {
"question" : question,
"option1" : option1,
"option2" : option2,
"option3" : option3,
"option4" : option4,
};
Read about Sets
Lists
and Maps
.
A list uses [ ]
But both map and set use curly braces {}
.
A set like a list,but with unique items, doesn't contain duplicates.
Upvotes: 1