Search code examples
flutterdartnullinitializationprovider

The argument type 'String?' can't be assigned to the paramter type 'String'


I'm new to programming and dont know anything. But right now, im trying to use Provider library at flutter and following some guy at youtube. But i got stuck at this situation. At first, flutter tell me to add late code, but it keep making my app error (LateInitializationError: Frield '_data@*****' has not been initialized.) So im trying to figure out this problem and i think it's about null / unnull something? (please explain this too :3) Anyway here's my code:

example_provider.dart

//Before 
//What's the difference between foundation and cupertino?
import 'package:flutter/foundation.dart';
// What is ChangeNotifier?
class ExampleProvider extends ChangeNotifier{
// Why do we need "late"? 
  late String _data;
// What is void ? 
 void setDataString(String data) {
    this._data = data;
// What is notifyListeners(); ?
notifyListeners();
  }
// Why do we need return this._data ?
  String getDataString () {
    return this._data;
  }
}

As you can see, this is my before code. i add late at String_data; because flutter told me so. But @Christoper Moore said in this forum LateInitializationError: Field 'data' has not been initialized, got error

that we must remove the late and add Question Mark(?) at our variable. Then i followed the advice/answer and change my code. Here's my after code :

example_provider.dart

//After  
//What's the difference between foundation and cu
import 'package:flutter/foundation.dart';
// What is ChangeNotifier?
class ExampleProvider extends ChangeNotifier{
// Why do we need "late"? 
   String? _data;
// What is void ? 
 void setDataString(String data) {
    this._data = data;
// What is notifyListeners(); ?
notifyListeners();
  }
// Why do we need return this._data ?
  String? getDataString () {
    return this._data;
  }
}

And then this is my homepage (which where the error occured) code : home.dart

import 'package:chapter_5/application/example_provider.dart';
import 'package:chapter_5/presentation/second_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Consumer<ExampleProvider>(
      builder: (context, exampleProvider, _) => Scaffold(
        appBar: AppBar(
          title: Text("Latihan 5"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(exampleProvider.getDataString()),
              ElevatedButton(
                child: Text("Go To Dashboard"),
                onPressed: () {
                  exampleProvider.setDataString("0");
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) =>
                          SecondPage(judul: "Ayam", tulisan: "Kodok"),
                    ),
                  );
                },
                style:
                    ButtonStyle(overlayColor: MaterialStateProperty.resolveWith(
                  (states) {
                    return states.contains(MaterialState.pressed)
                        ? Colors.amber
                        : null;
                  },
                )),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Please answer my question inside my codes too so i can understand more about flutter. Thank you for answering/trying


Solution

  • Anyway i figured it out guys. If you following my question, there's a before code on example_provider.dart Using this code, and i'll have a new error saying Lateinilization.... and then, i deleted the late String _data; code into String _data = ""; since the null safety needed a value.

    //Before 
    
    import 'package:flutter/foundation.dart';
    class ExampleProvider extends ChangeNotifier{
      String _data = "";
     void setDataString(String data) {
        this._data = data;    notifyListeners();
      }
      String getDataString () {
        return this._data;
      }
    }
    

    Therefore my code is running without an error. Thank you me.