Faruk AYDIN
Faruk AYDIN

Reputation: 215

Flutter - Provider - setstate or markneedsbuild() called during build

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();  }); }

Upvotes: 5

Views: 9527

Answers (4)

Ahmed Hamdy
Ahmed Hamdy

Reputation: 11

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

Upvotes: 0

Milind Gour
Milind Gour

Reputation: 131

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 ..

Upvotes: 0

Khurram
Khurram

Reputation: 311

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

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

Now my app is working perfectly

Upvotes: -1

Mahmoud Ashour
Mahmoud Ashour

Reputation: 327

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.

Upvotes: 12

Related Questions