45

There is no error in this code but this error is coming after running, Can someone please provide an example code of what's needed to solve for the error below? "LateInitializationError: Field 'data' has not been initialized, got error"

This is my_home_page.dart.

import 'package:fi_digital_earn/DashBoards/PromoCode/provider/myHomePageProvider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          ChangeNotifierProvider<MyHomePageProvider>(
            create: (context) => MyHomePageProvider(),
            child: Consumer<MyHomePageProvider>(
              builder: (context, provider, child) {
                if (provider.data == null) {
                  // print("prov $provider.data");
                  provider.getData(context);
                  return Center(child: CircularProgressIndicator());
                }
                // when we have the json loaded... let's put the data into a data table widget
                return SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  // Data table widget in not scrollable so we have to wrap it in a scroll view when we have a large data set..
                  child: SingleChildScrollView(
                    child: DataTable(
                      columns: [
                        // DataColumn(
                        // label: Text('Verified'),
                        // tooltip: 'represents if user is verified.'),
                        DataColumn(
                            label: Text('Pin ID'),
                            tooltip: 'represents first pin id of the user'),
                        DataColumn(
                            label: Text('Pin Name'),
                            tooltip: 'represents pin name of the user'),
                        DataColumn(
                            label: Text('Used/Unused'),
                            tooltip: 'represents Used/Unused of the user'),
                        DataColumn(
                            label: Text('Date'),
                            tooltip: 'represents date of the user'),
                      ],
                      rows: provider.data.results
                          .map((data) =>
                              // we return a DataRow every time
                              DataRow(
                                  // List<DataCell> cells is required in every row
                                  cells: [
                                    // DataCell((data.verified)
                                    //     ? Icon(
                                    //         Icons.verified_user,
                                    //         color: Colors.green,
                                    //       )
                                    //     : Icon(Icons.cancel, color: Colors.red)),
                                    // I want to display a green color icon when user is verified and red when unverified
                                    DataCell(Text(data.pin_id)),
                                    DataCell(Text(data.pin_name)),
                                    DataCell(
                                      RaisedButton(
                                          onPressed: () {},
                                          color: data.used_pin == "1"
                                              ? Colors.green
                                              : Colors.red,
                                          shape: new RoundedRectangleBorder(
                                            borderRadius:
                                                new BorderRadius.circular(30.0),
                                          ),
                                          // shape: Border.all(
                                          //   color: Colors.purple,
                                          //   width: 2.0,
                                          // ),
                                          // splashColor: Colors.cyan,
                                          // highlightColor: Colors.blue,
                                          child: data.used_pin == "1"
                                              ? Text("Useed")
                                              : Text("Unused")),
                                    ),

                                    DataCell(Text(data.pin_date)),
                                  ]))
                          .toList(),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

This is my Provider part.

import 'dart:convert';
import 'package:fi_digital_earn/DashBoards/PromoCode/model/myData.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
class MyHomePageProvider extends ChangeNotifier {
  late MyData data;

  Future getData(context) async {
   
    var url = Uri.parse(
        'https://software.oceonicitsolution.com/hrms/v3/mlm_api/v1/PromoCodeApi.php');
    var response = await http.get(url);
    print("res${response.body}");
    // now we have response as String from local json or and API request...
    var mJson = json.decode(response.body);
    // print("mjson" + mJson);
    this.data = MyData.fromJson(mJson);
    // this.data = http.post(mJson) as MyData;
    this.notifyListeners(); // for callback to view
  }
}

This is my Model part

class MyData {
  List<Results> results = [];

  MyData.fromJson(Map<String, dynamic> json) {
    // previous = json['previous'];
    // next = json['next'];
    if (json['results'] != null) {
      results = <Results>[];
      json['results'].forEach((v) {
        results.add(new Results.fromJson(v));
      });
    }
  }
}

class Results {
  String pin_id = "";
  String pin_name = "";
  String used_pin = "";
  String pin_date = "";

  Results.fromJson(Map<String, dynamic> json) {
    pin_id = json['pin_id'];
    pin_name = json['pin_name'];
    used_pin = json['use_unuse'];
    pin_date = json['pin_date'];
  }
}

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Deepak
  • 1,664
  • 3
  • 19
  • 52

4 Answers4

143

You don't want a late variable, you want a nullable one. If you need to check if something is initialized, you should be using a nullable variable instead and your code is already set up to check for null.

Just change

late MyData data;

to

MyData? data;
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
13

You can change it to :

MyData data = MyData(); //by initializing it.
//or by making it nullable.
MyData? data;

Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
3

I had this problem when i try to compare late value in if statement. how i solved, i gave some not important value before statement. Problem has ben solved.

Eray Hamurlu
  • 657
  • 7
  • 9
-22

I faced the same issue while using the Google Maps API in flutter, the solution was to provide the correct permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
LW001
  • 2,452
  • 6
  • 27
  • 36