0

The Machinemodel instance from the Consumer is always null.

Why?

App

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Provider nested model demo',
        theme: ThemeData(
          // This is the theme of your application.
          primarySwatch: Colors.blue,
        ),
        home: ApplicationPage());
  }
}

ApplicationPage

 class ApplicationPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
       var newMachineEntity = new MachineEntity(1, "Power x1000"); //, engines);

        return Scaffold(
            appBar: AppBar(title: Text("Machine demo"), elevation: 6),
            body: ChangeNotifierProvider(create: (context) => new MachineModel(newMachineEntity), child: MachineWidget()));
      }
    }

Widget

class MachineWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<MachineModel>(builder: (context, machine, child) {

                            ////////////////////// machine instance is null /////////////

      return Container(
        child: Column( 
          children: <Widget>[
            TextFormField(
              initialValue: machine.name,
              onChanged: (value) => machine.name = value,
            ), 
          ],
        ),
      );
    });
  }
}

Model

class MachineModel extends ChangeNotifier {
  MachineEntity machineEntity;
  // List<EngineModel> engines;
  MachineModel(this.machineEntity) {
    //  engines = this.machineEntity.engines.map((eng) => new EngineModel(eng)).toList();
  }

  String get name => machineEntity.name;
  set name(String value) {
    machineEntity.name = value;
    notifyListeners();
  }
}

Entity

class MachineEntity {
  int id;
  String name;
  // List<EngineEntity> engines;
  MachineEntity(this.id, this.name); //, this.engines);
}
Pascal
  • 12,265
  • 25
  • 103
  • 195
  • What version of provider do you use? – Rémi Rousselet Feb 02 '20 at 20:09
  • 4.0.2 ! Interesting thing is it worked actually some days before. AFAIR I updated to new flutter sdk... – Pascal Feb 02 '20 at 20:17
  • 1
    I copy pasted your code and it runs fine on my machine. I was able to print out `print(machine.name)` // = "Power x1000" as well. In my Pubspec.yaml I put provider: ^4.0.0 – mskolnick Feb 02 '20 at 20:21
  • 1
    damn you are right with 4.0.0 it works! – Pascal Feb 02 '20 at 20:23
  • Never update to minor versions it could break your code! :P – Pascal Feb 02 '20 at 20:37
  • Make a bug report, I'll investigate – Rémi Rousselet Feb 02 '20 at 20:53
  • Salut @RémiRousselet I will make a bug report if you answer this: https://stackoverflow.com/questions/60030620/using-flutter-provider-with-removewhere-list-function-deletes-the-wrong-items Then we have worked both the same because "your" bug cost me some nerves :p – Pascal Feb 02 '20 at 21:12
  • I can't reproduce your issue neither on 4.0.0 nor on 4.0.0+2 – Rémi Rousselet Feb 02 '20 at 21:33
  • It sounds to me like maybe you just needed to do a Hot Restart. Also, setting the version ^4.0.0 would mean it's always on the latest minor version within the "4.-.-" range, so in reality my version is also "4.0.2". – mskolnick Feb 03 '20 at 00:34
  • Yes the bug... is something different. In my real project - not the repro code here - I switched to 4.0.0 and the consumer`s model is still NULL. I can even do Hot Reload that never changes something in the UI. For every s*** I have to restart... But why got it fixed for my repro code ??? Probably a real refresh in the state :P – Pascal Feb 03 '20 at 19:32
  • Ok... after doing many restarts, delete the emulator, created new one , shaked my screen...I experienced now and then the null consumer error and other crazy stack overflow shit. FIRST I need to fix that shit dart hot reload which seems going mad on my pc. Thanks for listening guys... – Pascal Feb 03 '20 at 20:19
  • @ALL does your Hot Reload work ONLY for TEXT like strings or also for dart functions etc... ? For me it reloads only for strings... – Pascal Feb 03 '20 at 20:36
  • Man, I've been on this for an hours now. Works on 4.0.0, I was using 4.0.5. – Basanth Apr 27 '20 at 21:42

0 Answers0