Flutter editor shows an error when I try to add an item to list

Sajjad Emad

I'm trying to create a list in flutter then add items to it . But the problem is I'm facing an error in the editor (Android Studio) . It says "Invalid constructor name" .

Here's my code format :

List<String > list = new List<String>();
  list.add(" ");
Spatz

The error says that you are trying to write a method call in the class body. You should add items to the list in the body of the appropriate method. Only constructor calls are allowed in the class body.

class Wrong {
  List<String > list = new List<String>();
  list.add(""); // error: Invalid constructor name
}

class Ok {
  List<String > list = new List<String>();
  initState() {
    list.add("");
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Shows error "Cannot POST" when I try to submit the HTML form

Response returned by Route53 shows no Error, yet when I try to acess on browser, no response

Microsoft VS Code: When I try to launch my program error "spawn php ENOENT" shows up

Error when i try to opendir

Flutter: Error when trying to add data into a Map List

Flutter add item to list

I get error when i try to add value to defaultdict(str)

When i try to add ZXing.Net.Mobile.forms in xamrin forms and compile then it shows errors

Shows error “Cannot POST” when I try to submit the HTML form

Im getting an error everytime i try to add a value to my list

How to Add an item of a List from a class into another List when Text of the RaisedButton is changed on Flutter

When I add script code in wordpress' text editor, nothing shows up

Why does MYSQL error when I try to add a foreign key?

Error when I try to add an image web service prestashop

When I try to install vscode, snapd shows error code 400

Error when I try to add a dateTime field to my table

I am getting following error when I try to add constraint

when I try to save without image it shows the following error " "message": "Cannot read property 'path' of undefined".?"

When I try to use firebase in my flutter app it produces an error

NullPointerException when i try to add a object from a jpa repository to a list

when i try to change column name for max(value) inside a view in sql it shows error

How can I add an new property to a list item in flutter / dart?

This Error will be shown when I'll try to run flutter project

Pandas DataFrame shows cells to be strings, but returns an error when I try to split cells

Flutter: Error: Instance of Object when I try to update user data

Key error in dictionary when try to add a list element

When I try to add "scale_shape_manual", it shows up error "All unnamed arguments must be length 1"

System.NullReferenceException when i try to add a value to my List

flutter shows null in list view but when printed it shows the data