user16712640
user16712640

Reputation:

Flutter: NotifyListeners does not seem to be working

I have a TextField in which I display a value. Then I have a FAB that allows me to add a value to a SQFlite database through an AlertDialog. When I click the add value button on the TextField it doesn't get updated, I have to reopen the app to see the changes. Why? Am I doing something wrong?

In DBHelper class, methods to add a value and read all values:

Future create(Bmi bmi) async {
    final db = await instance.database;

    final id = await db.insert(tableBmi, bmi.toJson());
    log(bmi.toString());
    return bmi.copy(id: id);
  }

Future<List<Bmi>> readAllBmi() async {
    final db = await instance.database;

    final orderBy = '${BmiFields.dateTime} DESC';
    final result = await db.query(tableBmi, orderBy: orderBy);
    return result.map((json) => Bmi.fromJson(json)).toList();
  }

Method in the BmiListViewModel to add and to read:

List<BmiViewModel> bmiVm = [];

  void readAllBmi() async {
    List<Bmi> bmiList = await DbHelper.instance.readAllBmi();
    loadingStatus = BmiLoadingStatus.loading;
    notifyListeners();
    this.bmiVm = bmiList.map((bmi) => BmiViewModel(bmi: bmi)).toList();
    if (this.bmiVm.isEmpty) {
      this.loadingStatus = BmiLoadingStatus.completed;
    } else {
      this.loadingStatus = BmiLoadingStatus.completed;
    }
    notifyListeners();
  }

  addNewBmi(Bmi bmi) async {
    log(bmi.value);
    await DbHelper.instance.create(bmi);
    notifyListeners();
  }

main.dart:

return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => BmiListViewModel()),
      ],
      child: MaterialApp(
        title: 'BodyM',
        theme: ThemeData(
            canvasColor: Color(0xFF1C1C1E),
            primarySwatch: Colors.blue,
            textTheme:
            GoogleFonts.interTextTheme(Theme.of(context).textTheme)),
        initialRoute: '/',
        routes: routes,
      ),
    );

TextButton to add:

TextButton(
          child: Text("Add", style: TextStyle(color: Colors.green)),
          onPressed: () async {
            //here i create my bmi object
              await Provider.of<BmiListViewModel>(context, listen: false).addNewBmi(bmi);
            } else {
              //TODO
            }
          },
        ),

Container that contains the Text widget for displaying the value:

child: Container(
        child: Padding(
          padding: EdgeInsets.all(20.0),
          child: Row(
            children: [
              if(bmiVm.toString() == '[]')
                Text('N/A', style: TextStyle(color:Colors.white, fontSize: 80))
              else
                Text(bmiVm[0].valueBmi, style: TextStyle(color:Colors.white, fontSize: 80))
            ],
          ),
        ),
      )

bmiVm it's a List<'BmiViewModel'> create before the build method.

SOLUTION: I solved it. After addNewBmi method I have to call readAllBmi method to update the list and then notifyListeners() does the rest.

Upvotes: 1

Views: 1901

Answers (1)

Priya
Priya

Reputation: 15

TextButton(
      child: Text("Add", style: TextStyle(color: Colors.green)),
      onPressed: () async {
        //here i create my bmi object
          await Provider.of<BmiListViewModel>(context, listen: **true**).addNewBmi(bmi);
        } else {
          //TODO
        }
      },
    ),

You have to listen to the state changes inorder to update UI.

Upvotes: 0

Related Questions