This is my code:
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Future<ImageProvider> userImageFuture;
LoggedUser loggedUser = Cache.getLoggedUser();
@override
void initState() {
userImageFuture = Buttons.getUserImage(loggedUser.Id, context);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: SingleChildScrollView(
child: SafeArea(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 15, left: 15),
child: FutureBuilder<ImageProvider>(
future: userImageFuture,
builder: (BuildContext context,
AsyncSnapshot<ImageProvider> snapshot) {
return CircleAvatar(
radius: 20,
foregroundImage: snapshot.data,
backgroundImage: Settings.DefaultUserImage,
);
},
),
),
...
],
),
...
],
),
),
),
);
}
}
The problem is that FutureBuilder runs twice. It is supposed to fetch user profile picture from async method from another class:
class Buttons {
static Future<ImageProvider> getUserImage(
int userId, BuildContext context) async {
...
return image;
}
}
I followed this instructions. I edited my code and as far as I understand it from the docs:
The future must have been obtained earlier, e.g. during State.initState, State.didUpdateConfig, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.
I am relatively new to Flutter but I am convinced that this Future is not created at the same time as FutureBuilder.
Any possible solutions? I would be very grateful, because I've spent too much time on this. Thanks.