0

For prevent rebuild, I use a future instance _futureInstance in FutureBuilder:

var _futureInstance;

Future _loadData() async {
    // ...
}

@override
void initState() {
    super.initState();
    _futureInstance = _loadData();
}

@override
Widget build(BuildContext context) {
    body: FutureBuilder(
        future: _futureInstance,
        // ...
    )
}

It works well, now if the _loadData method has a parameter, such as _loadData(int i), how can I pass the parameter i to _futureInstance?

Mingfei
  • 1,459
  • 1
  • 10
  • 12

4 Answers4

1

I solved this problem in another way, use MaterialApp.onGenerateRoute.

As this answer: Flutter: Get passed arguments from Navigator in Widget's state's initState

I get the parameter in initState method:

  @override
  void initState() {
    super.initState();
    _futureInstance = _loadData(widget.parameter_key);
    // ...

and in build, the code not change:

@override
Widget build(BuildContext context) {
    body: FutureBuilder(
        future: _futureInstance,
        // ...
    )
}

In this way, the method _loadData can use various parameters, and the widget is not rebuild when setState is called.

Mingfei
  • 1,459
  • 1
  • 10
  • 12
-1
@override
void initState() {
    super.initState();
    _futureInstance = (int i) => _loadData(i);
}

@override
Widget build(BuildContext context) {
    body: FutureBuilder(
        future: _futureInstance(1), // pass your number here
        // ...
    )
}
Rajitha Udayanga
  • 1,559
  • 11
  • 21
  • Yes, in the way I can pass the parameter, but each time `setState` called, the widget rebuiled and the `_loadData` is recalled... – Mingfei Nov 24 '20 at 02:45
-1

wasn't sure the purpose of _futureInstance, perhaps you don't need to define the instance?

Future _loadData(val) async {
    // ...
}

@override
void initState() {
    super.initState();
}

@override
Widget build(BuildContext context) {
    body: FutureBuilder(
        future: _loadData(1),
        // ...
    )
}
xion
  • 1,209
  • 11
  • 17
  • The OP already explained why this is exactly what they don't want on a different answer. – Christopher Moore Nov 24 '20 at 04:47
  • i think i missed that very first line, which instead OP should be using streambuilder instead of futurebuilder. FutureBuilder don't mean to be rebuild but streambuilder does. – xion Nov 24 '20 at 06:21
  • You still misunderstand. "Yes, in the way I can pass the parameter, but each time setState called, the widget rebuiled and the _loadData is recalled." When `setState` is called, `_loadData` will be recalled which is undesirable behavior. – Christopher Moore Nov 24 '20 at 13:44
-1

i would approach like this


var _lastInt = 0;
var _lastFuture;


Future _loadData(int i) async {
  if(_lastInt!=i||_lastFuture==null){
    //do work
    _lastInt = i;
    _lastFuture = ;
  }
  return _lastFuture;
}


@override
Widget build(BuildContext context) {
  body: FutureBuilder(
    future: _loadData(1),
    // ...
  );

Yadu
  • 2,979
  • 2
  • 12
  • 27