Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Looking up a deactivated widget's ancestor is unsafe - reading or writing tags #89

Open

Description

@lim3ra

Hi all,

I am getting error everytime I want to scan NFC tag and go to other screen or write something to tag itself.

E/flutter (21076): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe. E/flutter (21076): At this point the state of the widget's element tree is no longer stable. E/flutter (21076): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method. E/flutter (21076): #0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3864:9) E/flutter (21076): #1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3878:6) E/flutter (21076): #2 Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:3926:12) E/flutter (21076): #3 Navigator.of (package:flutter/src/widgets/navigator.dart:2706:40) E/flutter (21076): #4 _DashboardScreenState.build.<anonymous closure> (package:Name/screens/dashboard_screen.dart:86:19) E/flutter (21076): #5 _rootRunUnary (dart:async/zone.dart:1362:47) E/flutter (21076): #6 _CustomZone.runUnary (dart:async/zone.dart:1265:19) E/flutter (21076): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7) E/flutter (21076): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11) E/flutter (21076): #9 _DelayedData.perform (dart:async/stream_impl.dart:591:14) E/flutter (21076): #10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11) E/flutter (21076): #11 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7) E/flutter (21076): #12 _rootRun (dart:async/zone.dart:1346:47) E/flutter (21076): #13 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (21076): #14 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (21076): #15 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1202:23) E/flutter (21076): #16 _rootRun (dart:async/zone.dart:1354:13) E/flutter (21076): #17 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (21076): #18 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (21076): #19 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1202:23) E/flutter (21076): #20 _microtaskLoop (dart:async/schedule_microtask.dart:40:21) E/flutter (21076): #21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Code:

class ListScreen extends StatefulWidget {

static const routeName = '/list-screen';

@override
_ListScreenState createState() => _ListScreenState();
}

class _ListScreenState extends State {

bool _isInit = true;
bool _isLoading = false;
StreamSubscription _stream;
bool _reading = true;
bool _brokenNfc = false;

@override
void initState() {
super.initState();
}

@override
void didChangeDependencies() async {

if (_isInit) {
  setState(() {
    _isLoading = true;
  });
}

await Provider.of<Things>(context, listen: false).fetchAndSetThings().then((_) async {
  setState(() {
    _isLoading = false;
    _isInit = false;
  });
});

super.didChangeDependencies();
}

@override
Widget build(BuildContext context) {

final locationsData = Provider.of<Locations>(context, listen: false);
final locations = locationsData.locations;

**_stream = NFC.readNDEF().listen((NDEFMessage message) {

  var parsedUri = Uri.parse(message.payload);
  String thingNfcId = parsedUri.queryParameters['nfc_id'];
  _stream?.cancel();

  if (thingNfcId == null || thingNfcId.isEmpty) {
    setState(() {
      _brokenNfc = true;
      //TODO: Show message for broken NFC tag.
    });
  } else {
    Navigator.of(context).pushNamedAndRemoveUntil(
      ThingDetailScreen.routeName,
      ModalRoute.withName('/Things-list-screen'),
      arguments: {
        ThingDetailsArgument.idType : ThingDetailsIdType.nfcId,
        ThingDetailsArgument.id : thingNfcId,
      },
    );
  }
}, onError: (e) {
  //TODO: Check errors.
});**

return DefaultTabController(
    length: locations.length,
    child: Scaffold(
        appBar: AppBar(
          leading:  BackButton(
            color: Theme.of(context).accentColor,
            onPressed: () {
              Navigator.of(context).pop(context);
            },
          ) ...

Anyone can suggest a solution? I suspect this is some logical error but I cannot diagnose it

Activity

  1. lim3ra commented on May 24, 2021

    @lim3ra
    Author

    At the beginning, I thought it was connected with Navigator but now - it shows an error even without it. After each NFC scan or write - I've got this:

    [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
    Minimal code to reproduce:

    import 'package:flutter/material.dart';
    import 'package:nfc_in_flutter/nfc_in_flutter.dart';
    import 'dart:async';
    
    class ListScreen extends StatefulWidget {
    
    static const routeName = '/list-screen';
    
    @override
    _ListScreenState createState() => _ListScreenState();
    }
    
    class _ListScreenState extends State {
    
    @override
    Widget build(BuildContext context) {
    
    StreamSubscription _stream;
    
    _stream = NFC.readNDEF().listen((NDEFMessage message) {
    
    var parsedUri = Uri.parse(message.payload);
    String nfcId = parsedUri.queryParameters['nfc_id'];
    _stream?.cancel();
    
    }, onError: (e) {
    //TODO: Check errors.
    });
    
    return Scaffold(
    body: Container(),
    );
    }
    }

    Flutter doctor:

    [✓] Flutter (Channel stable, 2.0.6, on macOS 11.3.1 20E241 darwin-x64, locale en-GB)
    • Flutter version 2.0.6 at /Users/lim3ra/Documents/Mobile dev/flutter
    • Framework revision 1d9032c (4 weeks ago), 2021-04-29 17:37:58 -0700
    • Engine revision 05e680e202
    • Dart version 2.12.3
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/lim3ra/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    ✗ Android license status unknown.
    Run flutter doctor --android-licenses to accept the SDK licenses.
    See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
    
    [✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
    Download at: https://developer.apple.com/xcode/download/
    Or install Xcode via the App Store.
    Once installed, run:
    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
    CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
    Without CocoaPods, plugins will not work on iOS or macOS.
    For more info, see https://flutter.dev/platform-plugins
    To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
    
    [✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
    🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
    🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    
    [✓] Connected device (2 available)
    • M2006C3MNG (mobile) • NVTOYDTOPNQCDMKJ • android-arm • Android 10 (API 29)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212
    
    ! Doctor found issues in 2 categories.
    lim3ra@Patryks-MacBook-Pro name % flutter doctor -v
    [✓] Flutter (Channel stable, 2.0.6, on macOS 11.3.1 20E241 darwin-x64, locale en-GB)
    • Flutter version 2.0.6 at /Users/lim3ra/Documents/Mobile dev/flutter
    • Framework revision 1d9032c (4 weeks ago), 2021-04-29 17:37:58 -0700
    • Engine revision 05e680e202
    • Dart version 2.12.3
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/lim3ra/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    ✗ Android license status unknown.
    Run flutter doctor --android-licenses to accept the SDK licenses.
    See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
    
    [✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
    Download at: https://developer.apple.com/xcode/download/
    Or install Xcode via the App Store.
    Once installed, run:
    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
    CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
    Without CocoaPods, plugins will not work on iOS or macOS.
    For more info, see https://flutter.dev/platform-plugins
    To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
    
    [✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
    🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
    🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    
    [✓] Connected device (2 available)
    • M2006C3MNG (mobile) • NVTOYDTOPNQCDMKJ • android-arm • Android 10 (API 29)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212
    
    ! Doctor found issues in 2 categories.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions