This repository was archived by the owner on Jun 23, 2024. It is now read-only.
Repository navigation
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
Copy link
Copy link
Open
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
lim3ra commentedon May 24, 2021
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:
Flutter doctor: