H
H
hubble2021-01-27 13:41:44
Dart
hubble, 2021-01-27 13:41:44

Why is the widget being rebuilt multiple times when passing arguments through the Navigator?

This is the code with the table:

spoiler

import 'package:flutter/material.dart';
    import 'package:csv/csv.dart';
    import 'dart:io';
    
    class ShowTable extends StatefulWidget {
      @override
      _ShowTableState createState() => _ShowTableState();
    }
    
    class _ShowTableState extends State<ShowTable> {
      List<List<dynamic>> data = [];
    
      void _load() async {
        // String linkOfDict = ModalRoute.of(context).settings.arguments;
        String linkOfDict =
            '/data/data/com.example.eng_translator/app_flutter/sales.csv';
    
        String contents = await File(linkOfDict).readAsString();
        data = CsvToListConverter().convert(contents);
    
        setState(() {});
    
        print(data);
      }
    
      @override
          void initState() {
          super.initState();
          _load();
       }

      @override
      Widget build(BuildContext context) {
        // _load();
    
        return Scaffold(
          appBar: AppBar(
            title: Text("Table"),
          ),
          body: SingleChildScrollView(
            child: Table(
              columnWidths: {
                0: FlexColumnWidth(1),
              },
              border: TableBorder.all(width: 1.0),
              children: data.map((item) {
                return TableRow(
                    children: item.map((row) {
                  return Container(
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        row.toString(),
                        style: TextStyle(fontSize: 20.0),
                      ),
                    ),
                  );
                }).toList());
              }).toList(),
            ),
          ),
        );
      }
    }


Passing an argument:
Navigator.pushNamed(context, '/show_table', arguments: linkOfDict);

When the variable linkOfDict is hardcoded , the table appears without problems and print (which is print(data); ) is written once to the console, as it should be. Everything is fine with this option. But if I set linkOfDict not rigidly, but get it via ModalRoute (commented out in the code), then I get the following errors:

E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: dependOnInheritedWidgetOfExactType <_ModalScopeStatus>() or dependOnInheritedElement() was called before _ShowTableState.initState() completed.
E/flutter ( 4289): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
E/flutter ( 4289): Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.


When the _load() method is used in build(BuildContext context) rather than initState() , the table is drawn, but hereprint(data) is updated very fast. And it doesn't matter if linkOfDict is hardcoded or received via ModalRoute

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2021-01-27
@hubbletvv

setState causes the widget to be redrawn, respectively, Widget build(BuildContext context) is called, in which you call the redraw again

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question