skip to Main Content

I get this error after writing this code. Non-nullable instance field ‘_cardResults’ must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it. When I try to add the late initializer and run it I get another error: LateInitializationError: Field ‘_cardResults’ has not been initialized.

import 'dart:async';
import 'package:rxdart/rxdart.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import '../../helpers/card_colors.dart';
import '../../models/credit card/card_model.dart';

class CardListBloc {
final BehaviorSubject<List<CardResults>> _cardsCollection =
  BehaviorSubject<List<CardResults>>();

List<CardResults> _cardResults;

//Retrieve data from Stream
Stream<List<CardResults>> get cardList => _cardsCollection.stream;

void initialData() async {
var initialData = await rootBundle.loadString('data/initialData.json');
var decodedJson = jsonDecode(initialData);
_cardResults = CardModel.fromJson(decodedJson).results;
for (var i = 0; i < _cardResults.length; i++) {
  _cardResults[i].cardColor = CardColor.baseColors[i];
}
_cardsCollection.sink.add(_cardResults);
}

 CardListBloc() {
initialData();
}

void addCardToList(CardResults newCard) {
_cardResults.add(newCard);
_cardsCollection.sink.add(_cardResults);
}

void dispose() {
_cardsCollection.close();
}
}

final cardListBloc = CardListBloc();

2

Answers


  1. Chosen as BEST ANSWER

    i fixed this problem by adding this._cardResult and calling empty list in final cardList like so:

    CardListBloc(this._cardResults) {
    
    initialData();
     }
     final cardListBloc = CardListBloc([]);
    

  2. The problem is that you’re trying to access the _cardResults field, which is a non-nullable instance field before it has been initialized. In Dart, non-nullable instance fields must be initialized before they can be used.

    To resolve this issue, you can either initialize the _cardResults field when it’s declared or initialize it in the constructor. let me show you an example of initializing it in the constructor:

    class CardListBloc {
      final BehaviorSubject<List<CardResults>> _cardsCollection =
          BehaviorSubject<List<CardResults>>();
    
      List<CardResults> _cardResults;
    
      // Retrieve data from Stream
      Stream<List<CardResults>> get cardList => _cardsCollection.stream;
    
      void initialData() async {
        var initialData = await rootBundle.loadString('data/initialData.json');
        var decodedJson = jsonDecode(initialData);
        _cardResults = CardModel.fromJson(decodedJson).results;
        for (var i = 0; i < _cardResults.length; i++) {
          _cardResults[i].cardColor = CardColor.baseColors[i];
        }
        _cardsCollection.sink.add(_cardResults);
      }
    
      CardListBloc() {
        _cardResults = []; // Initialize your field here...
        initialData();
      }
    
      void addCardToList(CardResults newCard) {
        _cardResults.add(newCard);
        _cardsCollection.sink.add(_cardResults);
      }
    
      void dispose() {
        _cardsCollection.close();
      }
    }
    
    final cardListBloc = CardListBloc();
    

    In my example above, the _cardResults field is initialized as an empty list in the constructor before calling the initialData() method. This ensures that the field is initialized before it’s used.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search