I know that there are a lot of similar questions but none helped me, that's why I'm posting this one, so please bear with me.
My screen is:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:manager/components/account.dart';
import 'package:manager/components/roundRectButton.dart';
import 'package:manager/constants.dart';
import 'package:manager/services/networking.dart';
// NetworkHelper networkHelper = NetworkHelper();
List<RoundRectButton> navigation = [
RoundRectButton(
'Volumes', () => _DashboardState().updateUI(Account().returnedData())),
];
class Dashboard extends StatefulWidget {
@override
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<Dashboard> {
dynamic result;
dynamic updateUI(fun) async {
ListView temp = await fun;
result = temp;
// setState(() {
// result = temp;
// });
}
@override
void initState() {
setState(() {
result = Text('Hello');
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: navigation,
),
),
Divider(
color: kGreenColor,
),
Padding(
padding: EdgeInsets.all(10),
child: result,
),
],
),
),
);
}
}
With _DashboardState().updateUI(Account().returnedData())
I return an Instance of 'Future', which I turn to ListView in updateUI.
If I print temp in updateUI I get:
ListView(scrollDirection: vertical, primary: using primary controller, AlwaysScrollableScrollPhysics, shrinkWrap: shrink-wrapping)
so I guess this works so far.
The problem is when I call setState() in updateUI, I get the error
E/flutter ( 5156): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: setState() called in constructor: _DashboardState#6c793(lifecycle state: created, no widget, not mounted)
E/flutter ( 5156): This happens when you call setState() on a State object for a widget that hasn't been inserted into the widget tree yet. It is not necessary to call setState() in the constructor, since the state is already assumed to be dirty when it is initially created.
E/flutter ( 5156): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1255:9)
E/flutter ( 5156): #1 State.setState (package:flutter/src/widgets/framework.dart:1266:6)
E/flutter ( 5156): #2 _DashboardState.updateUI (package:manager/screens/dashboard.dart:32:5)
E/flutter ( 5156): <asynchronous suspension>
Similar code works perfectly in another project so it drives me crazy.