skip to Main Content

The non-nullable local variable ‘product’ must be assigned before it can be used.
Try giving it an initializer expression, or ensure that it’s assigned on every execution path.
But the way I thought it ends up giving error

//product_model_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'package:app/models/product/product_model.dart';

void main() {
  testWidgets('Test id', (tester) async {

    ProductModel product;
    print(product.id); //gives this error in product - The non-nullable local variable 'product' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path.
  });
}

//product_model.dart

class ProductModel{
  ProductModel.fromWeb(Map data) {
    id = data['id'];
    description= data['description'];
    icon= data['icon'];
  }

  late final int id;
  late final String description;
  late final String? icon;
}

2

Answers


  1. Chosen as BEST ANSWER
    void main() {
      test('Test id', () {
        ProductModel product = ProductModel.fromWeb({{"id": 1, "description": "Some description", "icon": null }});
        print(product.id);
      });
    }
    

    In this way the error was solved

    • Read the error comments, to understand better

  2. You should assign a value for ProductModel product before using it.

    Like:

      ProductModel product = ProductModel.fromWeb(someMap);
    

    If you want to tell dart that the variable hasn’t been initialized yet and it may not have a ProductModel value when called, you should type it as:

      ProductModel? product;
      if (product != null) print(product!.id); // the "!" tells dart that you think this variable is not null here
    

    Then if you want to use a parameter from a nullable class, you can do the following:

    print(product?.id) //prints either the id or null
    print(product?.id ?? 0) //prints either the id or (if the expression on the left is null) 0
    

    This tells Dart that your variable can be null;
    Or, if you know that this variable will never be null, but you’ll assign it a value before using it, you can use the late modifier as in your class’ parameters.

    Dart is now a null-safe language so this ? is how you tell a variable that it can be null

    PS:
    About your error on the comments above type 'Null' is not a subtype of type 'int'

    The same applies to your inner variables. They should be int? if you are not sure that the given Map will give you the value for that key. Or you could do something like:

    class ProductModel{
      ProductModel.fromWeb(Map data) {
        id = data['id'] ?? 0; //will automatically set the value to 0 if the map didn't have that key or the value for it was null
        description= data['description'] ?? ''; //will automatically set the value to '' if the map didn't have that key or the value for it was null
        icon= data['icon'];
      }
    
      final int id;
      final String description;
      final String? icon;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search