How to fix the problem of Do not use BuildContexts across async gaps.
?
I have seen other solutions of how to fix this but those solutions didn't look relevant to the context by which I have recieved the error. I have tried a multitude of ways to fix the warning, but its still there. Any help would be appreciated.
import 'network.dart';
import 'package:demo/location.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
void main() {
runApp(
const MaterialApp(
home: Loading(),
),
);
}
class Loading extends StatefulWidget {
const Loading({Key? key}) : super(key: key);
@override
State<Loading> createState() => _LoadingState();
}
class _LoadingState extends State<Loading> {
@override
void initState() {
super.initState();
getLocationData();
}
void getLocationData() async {
var network = await LocationData().getLocationWeather();
Navigator.push( //The problem is here.
context,
MaterialPageRoute(
builder: (context) {
return Location(
locationData: network,
);
},
),
);
}
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: SpinKitWanderingCubes(
color: Colors.white,
size: 100,
),
),
),
);
}
}