0

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,
          ),
        ),
      ),
    );
  }
}
  • 1
    ```void getLocationData() async { var network = await LocationData().getLocationWeather(); if (!mounted) return; Navigator.push( //The problem no longer here.``` – Per.J Jun 20 '22 at 09:50
  • 1
    Can you include more about `LocationData().getLocationWeather()` – Md. Yeasin Sheikh Jun 20 '22 at 10:37
  • 1
    Did you check this question which is same as your - https://stackoverflow.com/questions/68871880/do-not-use-buildcontexts-across-async-gaps – Harish Sharma Jun 20 '22 at 10:48

0 Answers0