A value of type 'Set<String?>' can't be assigned to a variable of type 'Map<String, String>'

Bhaskar2510

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,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
caiopo

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

A value of type 'Future<String>' can't be assigned to a variable of type 'String'

Error: A value of type 'String?' can't be assigned to a variable of type 'String'

Error: A value of type 'String?' can't be assigned to a variable of type 'num'

Error: A value of type 'String' can't be assigned to a variable of type 'int'

Error 'A value of type 'dynamic' can't be assigned to a variable of type 'String'.'

A value of type 'int' can't be assigned to a variable of type 'String'

Flutter - A value of type 'List<Map<String, Object>>' can't be assigned to a variable of type 'List<Classes>'

A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>'

A value of type 'List<Map<String, dynamic>>' can't be assigned to a variable of type 'List<ProjectBoxes>'

A value of type 'Future<String>' can't be assigned to a variable of type 'String'. Try changing the type of the variable

The argument type 'Set<String>' can't be assigned to the parameter type 'Map<String, dynamic>'

flutter: A value of type 'Future<String>' can't be assigned to a variable of type 'String'

Converting JSON data to a list view results in an error of A value of type 'List<XXX>' can't be assigned to a variable of type 'Map<String, dynamic>'

The argument type Set<Map<String, Widget Function(BuildContext)>> can't be assigned to the parameter type 'Map<String, Widget Function(BuildContext)>

Error 'A value of type 'dynamic' can't be assigned to a variable of type 'String'.' in Dart 2.2

A value of type 'List<dynamic>' can't be assigned to a variable of type 'List<String>'

Argument 'Set<Future<DocumentReference<Map<String, dynamic>>>> Function(String)' can't be assigned to parameter type 'void Function(String, dynamic)'

The argument type 'Map<String, Object>' can't be assigned to the parameter type 'String'.dartargument_type_not_assignable

The argument type 'Map<String, dynamic> Function()' can't be assigned to the parameter type 'Map<String, dynamic>'

The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to the parameter type 'List<Map<String, dynamic>>'

Set<Map<String, dynamic>>' can't be assigned to the parameter type 'Map<String, dynamic> error in Flutter when writing to Firestore

Flutter Dart - the argument type 'String' can't be assigned to the parameter type 'Map<String,dynamic>'

error using riverpod to The argument type 'String' can't be assigned to the parameter type 'Map<String, dynamic>'

Dart - The argument type 'String' can't be assigned to the parameter type 'String'

The argument type String? can't be assigned to the parameter type String

The argument type 'String' can't be assigned to the parameter type 'String'

The argument type 'String"?"' can't be assigned to the parameter type 'String'

Argument type String? can't be assigned to the parameter type 'String'

Error: The argument type 'String?' can't be assigned to the parameter type 'String'