Flutter - Provider - setstate or markneedsbuild() called during build
Asked Answered
W

4

5

I have a project with Flutter. I am using Provider (4.0.5) for state management. I am getting this warning, "setstate or markneedsbuild() called during build" This is big problem for me. I think this problem will be grow in release mode. How can I fix this problem?

import 'package:example/models/notebook_model.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class NotebookState with ChangeNotifier {

  DateTime notebookDate = DateTime.now();

  DateTime notebookDateState(DateTime date){
     notebookDate = date;
     notifyListeners();
     return notebookDate;
  }

  Future<List<NotebookModel>> getNotebook()
  {
    notifyListeners();
    return NotebookBLL.getNotebooks();
  }

  addNotebook(String noteTitle, String content, String date){
    NotebookModel newNotebook = NotebookModel(
        noteTitle: noteTitle, content: content, date: date);

    NotebookBLL.insert(newNotebook);

    notifyListeners();                    
  }

  updateNotebook(int id, String noteTitle, String content, String date){
    NotebookModel updateNotebook = NotebookModel(
      id: id,
      noteTitle: noteTitle,
      content: content,
      date: date
    );
    NotebookBLL.update(updateNotebook);

    notifyListeners();                    
  }

  deleteNotebook(int id){
    NotebookBLL.delete(id);
    notifyListeners();  
  }

}

This is my UI code

Consumer<NotebookState>(builder: (context, state, a) { 
             return FutureBuilder(
                     future: state.getNotebook(),
                     builder: (BuildContext context, snapshot) {
                       return snapshot.hasData
                                ? createNotebookList(context, snapshot.data)
                                : Container();  }); }
Worldly answered 27/4, 2020 at 15:11 Comment(0)
T
12

I guess you should remove notify listeners from the getNotebook() function. The reason is that notifyListener rebuilds your widget tree because it causes setState.And you can't rebuild widgets during it's being built with the Future Builder So you can't use setState (or notify listeners in this case) in a builder like you did.

Tswana answered 27/4, 2020 at 15:22 Comment(2)
Thank you!! You are amazing! It's worked for me! :)Worldly
So i removed notifyListeners() and it solved my issue? But I am confused on how I am still accessing the new value later? Is it because the stream builder is rebuilding the widgets?Maximamaximal
G
0

When I faced the issue , The problem was I was calling my provider class when widget was building , I called the class to update the var using notifylistners(); , due to which It rebuilts the whole widget tree but at the same time widget tree was building ..soo it cannot rebuild the tree which is not made yet ..hence the issue ..try to read the paragrapgh given in the error to solve your issue ..

Gustav answered 13/8, 2021 at 20:53 Comment(0)
I
0

You maybe calling notifyListener as soon as you call a provider method after creating it on initState method

Ingridingrim answered 25/6, 2022 at 15:5 Comment(0)
C
-1

i resolved this issue by removing this line of code from my init state

 context.read<CamerasProvider>().setCamerasList();

Now my app is working perfectly

Camillecamilo answered 30/6, 2021 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.