0

I am trying to insert a string into my list of strings.

I initialize my string like so in a class called globals that holds this list.

List<String> myStringList = [];

Then in my controller class I am trying to insert a string into the list like so.

globals.myStringList.add(stringToAdd);

But I get an error that says the list is null. Not sure if I am initializing it wrong since it appears this is the way to initialize an empty list.

Globals Class

import 'package:flutter_project/models/categories.dart';
import 'package:flutter_project/models/videos.dart';
List<String> reports = [];
List<Video> videos = [];
List<Category> categories = [];
List<String> userSearches = <String>[];
bool inSearch = false;
2
  • You have initialized it right but the matter is where you have initialized.Can you show the full code of that globals class Commented May 17, 2020 at 12:28
  • The globals class just holds some variables that I need to reference throughout the program. Commented May 17, 2020 at 14:29

2 Answers 2

3

Found the answer, I was initializing my string incorrectly. It has to be done like so.

List<String> myStringList = <String>[];

Sign up to request clarification or add additional context in comments.

4 Comments

This is the right way List<String> myStringList = [];-- How you found error here?
Yeah thought it was fixed but it turns out it's still throwing the error that it is null when I reference it for the first time.
Okay I think I have the answer, because the globals class isn't really initialized due to it just holding variables it's never really set equal to nothing. So I just run a check to see if it's null and if it is then I set it to empty and it appears to be working.
@poomulus thanks bro, for your question and answer.
-1

Make the variable static so that it can be accessible by the class name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.