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
You can’t call
setState
synchronously from theinitState
becausesetState
is triggering thebuild
method andinitState
is called before the first call tobuild
, 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: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 callingsetState
from youraddInvestor
method but you’ll have to define what should be the trigger that’ll calladdInvestor
.Code sample
Try the full code sample on DartPad
just add this code in your initState. this will only run once your widget tree is mounted