Home > Back-end >  Error in class Constructor in Flutter(VS Code)
Error in class Constructor in Flutter(VS Code)

Time:10-23

I am new to flutter so I am experimenting with it , I tried to create a custom class and the constructor of that class in vs code, I constantly keep on getting the error message on the constructor of the class. The error reads like this " Non nullable instance field author(which is the name of one of the attributes of the class) must be initialized idk what error is this I have tried various online resources but only to return in vain . A look at the code and a help in identifying the error would be really helpful.

Code:

import 'package:flutter/material.dart';
import 'quote_class.dart';

void main() {
  runApp(const MaterialApp(
    home: Home(),
    debugShowCheckedModeBanner: false,
  ));
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<Quotes> quotes = [
    Quotes(
         "Be yoursefl cause everyone else is taken","Oscar Wilde"),
    Quotes(
      "I have nothing to declare except my genius",  "Einstien"),
    Quotes(
        "The truth is rarely pure and never simple", "Priyanka"),
    Quotes(
          "Trust those who are still in the search of truth and not those who has already found one","Hey")
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.blue[400],
        appBar: AppBar(
          title: const Text("Awesome Quotes"),
          centerTitle: true,
          backgroundColor: Colors.purple[200],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: quotes
              .map((quote) => Text('${quote.text} - ${quote.author}'))
              .toList(),
        ));
  }
}

Code for the custom class where I am getting the error , I am trying to use the custom class in the above main.dart file


class Quotes {
  String text;
  String author;

  Quotes(String text, String author) {
    this.text = text;
    this.author = author;
  }
}



Error message:

lib/quote.dart:5:16: Error: The parameter 'text' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
  Quotes({this.text, this.author});
               ^^^^
lib/quote.dart:5:27: Error: The parameter 'author' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
  Quotes({this.text, this.author});
                          ^^^^^^

CodePudding user response:

you can use null safety in Quotes model and Just put a question mark before properties name

import 'package:flutter/material.dart';


void main() {
  runApp(const MaterialApp(
    home: Home(),
    debugShowCheckedModeBanner: false,
  ));
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<Quotes> quotes = [
    Quotes(
        "Be yoursefl cause everyone else is taken","Oscar Wilde"),
    Quotes(
        "I have nothing to declare except my genius",  "Einstien"),
    Quotes(
        "The truth is rarely pure and never simple", "Priyanka"),
    Quotes(
        "Trust those who are still in the search of truth and not those who has already found one","Hey")
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.blue[400],
        appBar: AppBar(
          title: const Text("Awesome Quotes"),
          centerTitle: true,
          backgroundColor: Colors.purple[200],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: quotes
              .map((quote) => Text('${quote.text} - ${quote.author}'))
              .toList(),
        ));
  }
}

class Quotes {
  String? text;
  String? author;

  Quotes(String text, String author) {
    this.text = text;
    this.author = author;
  }
}

output:

enter image description here

CodePudding user response:

does not consider the body of a constructor. Too much of a mess, too much is left to chance or too hard to determine. In dart you have a means of directly telling the compiler where you want the parameters to go, you don't have to spell it out in the body.

This is the class you want in your case:

class Quotes {
  final String text;
  final String author;

  const Quotes(this.text, this.author);
}

CodePudding user response:

It seems like your project is running with sound null safety ON.

So, for your class

class Quotes {
String text;
String author;

 Quotes(String text, String author) {
   this.text = text;
   this.author = author;
 }
}

the fields text and author must not be null(declaring them as String with question mark ? will make them null-able) and probably fix the error. However, if you want them to be non-nullable, you declare them as you have and add a 'late' keyword in-front of it which will let you have the text/author value null until ready.

The class would be as below

class Quotes {
late String text;
late String author;

Quotes(String text,String author) {
  this.text = text;
  this.author = author;
 } 
}
  • Related