Pana-g / flutter_earth_globe

MIT License
21 stars 6 forks source link

DartError: setState() called after dispose(): RotatingGlobeState#63df9(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers) #12

Closed Pabears closed 2 months ago

Pabears commented 2 months ago

hello, I have a NavigationBar and two tabs, the first tab is a list and other is the earth demo. when I switch to tab 2 (the earth) and switch back to tab 1, i got this error.

DartError: setState() called after dispose(): RotatingGlobeState#63df9(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers) This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose(). dart-sdk/lib/_internal/js_dev_runtime/private/ddcruntime/errors.dart 296:3 throw packages/flutter/src/widgets/framework.dart 1167:9 packages/flutter/src/widgets/framework.dart 1201:14 setState packages/flutter_earth_globe/rotating_globe.dart 558:43 dart-sdk/lib/async/future.dart 431:42 dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:11 internalCallback DartError: setState() called after dispose(): RotatingGlobeState#63df9(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers) This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose(). dart-sdk/lib/_internal/js_dev_runtime/private/ddcruntime/errors.dart 296:3 throw packages/flutter/src/widgets/framework.dart 1167:9 packages/flutter/src/widgets/framework.dart 1201:14 setState packages/flutter_earth_globe/rotating_globe.dart 558:43 dart-sdk/lib/async/future.dart 431:42 dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:11 internalCallback

and this is my demo Earth Page:

import 'package:flutter/material.dart';
import 'package:flutter_earth_globe/flutter_earth_globe.dart';
import 'package:flutter_earth_globe/flutter_earth_globe_controller.dart';
import 'package:flutter_earth_globe/globe_coordinates.dart';
import 'package:flutter_earth_globe/point.dart';
import 'package:vpm/main.dart';

class Earth extends StatefulWidget {
  Earth(this.servers);

  final List<Server> servers;

  @override
  State<Earth> createState() => _EarthState();
}

class _EarthState extends State<Earth> {
  late FlutterEarthGlobeController _controller;

  @override
  void initState() {
    _controller = FlutterEarthGlobeController(
        rotationSpeed: 0.2,
        zoom: 1.6,
        maxZoom: 3,
        isRotating: false,
        isBackgroundFollowingSphereRotation: false,
        background: Image.asset('assets/2k_stars.jpg').image,
        surface: Image.asset('assets/2k_earth-day.jpg').image);

    List<Point> points = widget.servers
        .map((server) => Point(
            coordinates: GlobeCoordinates(server.lat, server.lon),
            id: server.addr,
            style: PointStyle(
                color: const Color.fromARGB(255, 197, 245, 8), size: 10)))
        .toList();

    for (var point in points) {
      _controller.addPoint(point);
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: FlutterEarthGlobe(
      radius: MediaQuery.of(context).size.width < 500
          ? ((MediaQuery.of(context).size.width / 3.8) - 20)
          : 120,
      controller: _controller,
    ));
  }
}

thanks, I think there maybe some dispose lifecycle error in the controller.

Pabears commented 2 months ago

we could fixit in packages/flutter_earth_globe/rotating_globe.dart 558:43

if (changed) {
  Future.delayed(Duration.zero, () {
// todo if (!mounted) return;
    setState(() {});
  });
}
Pana-g commented 2 months ago

@Pabears This has been fixed in the latest version 1.0.5.