skip to Main Content
import 'package:flutter/material.dart';

class Investor extends StatefulWidget {
  @override
  State<Investor> createState() => InvestorState();
}

List<InvestorList> investorList = [];

class InvestorState extends State<Investor> {
  List<InvestorList> investorLIST = [];
  void addInvestor({required String name, required int taka}) {
    final investor = InvestorList(investorName: name, investorTaka: taka);
    investorLIST.add(investor);
    // setState(() {
    //   investorLIST;
    // });
    setState(() {});
  }

  List<Widget> widgetList() {
    List<Widget> widgetMakingList = [];
    setState(() {
      investorLIST;
    });
    for (int i = 0; i < investorLIST.length; i++) {
      widgetMakingList.add(Column(
        children: <Widget>[
          Text(
            '${investorLIST[i].investorName!}n'
            'investing amount:${investorLIST[i].investorTaka!} taka',
            style: const TextStyle(
                color: Colors.black87,
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                fontFamily: 'Times new Roman'),
          ),
          const SizedBox(
            height: 10,
          ),
          SizedBox(
            height: 5,
            child: Container(
              color: Colors.black87,
            ),
          ),
        ],
      ));
    }
    return widgetMakingList;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      investorLIST = investorList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 200,
      child: ListView(
        children: widgetList(),
      ),
    );
  }
}

class InvestorList {
  final String investorName;
  final int investorTaka;
  InvestorList({required this.investorName, required this.investorTaka});
}
import 'package:flutter/material.dart';

class Investor extends StatefulWidget {
  @override
  State<Investor> createState() => _InvestorState();
}

List<InvestorList> investorList = [];

class _InvestorState extends State<Investor> {

  void addInvestor({required String name, required int taka}) {
    final investor = InvestorList(investorName: name, investorTaka: taka);
    investorList.add(investor);
    // setState(() {
    //   investorList;
    // });
    setState(() {});
  }

  List<Widget> widgetList() {
    List<Widget> widgetMakingList = [];
    setState(() {
      investorList;
    });
    for (int i = 0; i < investorList.length; i++) {
      widgetMakingList.add(Column(
        children: <Widget>[
          Text(
            '${investorList[i].investorName!}n'
            'investing amount:${investorList[i].investorTaka!} taka',
            style: const TextStyle(
                color: Colors.black87,
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                fontFamily: 'Times new Roman'),
          ),
          const SizedBox(
            height: 10,
          ),
          SizedBox(
            height: 5,
            child: Container(
              color: Colors.black87,
            ),
          ),
        ],
      ));
    }
    return widgetMakingList;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      investorList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 200,
      child: ListView(
        children: widgetList(),
      ),
    );
  }
}

class InvestorList {
  final String investorName;
  final int investorTaka;
  InvestorList({required this.investorName, required this.investorTaka});
}

I am trying to do this that whenever the investorList is getting updated then the widget should be recreated. But it is not. I know that the investorList should be inside the class _InvestorState but investorList will be accessed from other classes.I have tried a lot how to update the widget,i have used setSate but it also didn’t help me. What can i do?

2

Answers


  1. You can’t call setState synchronously from the initState because setState is triggering the build method and initState is called before the first call to build, this is what is implied by "widget is not mounted".

    You can call setState only if your widget is mounted here’s what is said in the Flutter documentation:

    After creating a State object and before calling initState, the
    framework "mounts" the State object by associating it with a
    BuildContext. The State object remains mounted until the framework
    calls dispose, after which time the framework will never ask the State
    object to build again.

    It is an error to call setState unless mounted is true.

    You should be able to trigger a rebuild when your variable investorList is edited by calling setState from your addInvestor method but you’ll have to define what should be the trigger that’ll call addInvestor.

    Code sample

    class _InvestorState extends State<Investor> {
      final investorList = <InvestorList>[];
    
      @override
      Widget build(BuildContext context) {
        return SizedBox(
          height: 200,
          child: ListView(
            children: [
              for (final e in investorList) ...[
                Text(
                  '${e.investorName}n'
                  'investing amount:${e.investorTaka} taka',
                  style: const TextStyle(
                      color: Colors.black87,
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      fontFamily: 'Times new Roman'),
                ),
                const SizedBox(height: 10),
                const Divider(color: Colors.black87, thickness: 5),
              ],
            ],
          ),
        );
      }
    
      void addInvestor({required String name, required int taka}) {
        final investor = InvestorList(investorName: name, investorTaka: taka);
        investorList.add(investor);
        setState(() {});
      }
    }
    

    Try the full code sample on DartPad

    Login or Signup to reply.
  2. just add this code in your initState. this will only run once your widget tree is mounted

    if(mounted){
      setState(() {
       investorLIST = investorList;
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search