I am having an issue with flutter stream. I am using objectbox to store my data but the problem seems to be with flutter streaming. here is my class for objectbox
import 'package:finsec/features/dashboard/data/datasources/InheritedProvider.dart';
import 'package:finsec/features/income/data/models/Income.dart';
import '../../../../objectbox.g.dart';
import 'dart:async';
class ObjectBox {
/// The Store of this app.
late final Store store;
late final Box<Income> incomeBox;
/// A stream of all notes ordered by date.
late final Stream<Query<Income>> incomeQueryStream;
late final qBuilder;
ObjectBox._create(this.store) {
// Add any additional setup code, e.g. build queries.
incomeBox = Box<Income>(store);
qBuilder = incomeBox.query(Income_.monthNumber.equals(0) & Income_.calendarYear.equals(0));
incomeQueryStream = qBuilder.watch(triggerImmediately: true);
}
/// Create an instance of ObjectBox to use throughout the app.
static Future<ObjectBox> create() async {
// Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
final store = await openStore();
return ObjectBox._create(store);
}
}
here is partial code from my main class on how i call the object box class :
Future<void> main() async {
// This is required so ObjectBox can get the application directory
// to store the database in.
WidgetsFlutterBinding.ensureInitialized();
objectBox = await ObjectBox.create();
runApp(new MyHomePage( initialDate: DateTime.now()));
}
in the main home screen, i click a button which takes to another page which display my data from my database. here is partial code from my class
class IncomeTransaction extends StatefulWidget {
IncomeTransaction(
this.outstanding,
this.received_or_paid,
this.monthYear,
this.isSearch,
) ;
final String outstanding , received_or_paid;
bool isSearch;
MonthYear monthYear;
#override
State<StatefulWidget> createState() {
return IncomeTransactionState();
}
}
class IncomeTransactionState extends State<IncomeTransaction> {
int count = 0;
final String assetName = 'assets/images/exclamation_sign.svg';
final _listController = StreamController<List<Income>>.broadcast(sync: true);
//StreamController<List<Income>> _listController = new BehaviorSubject();
#override
void initState() {
super.initState();
setState(() {
_listController.addStream(objectBox.incomeQueryStream.map((q) {
q.param(Income_.monthNumber).value = widget.monthYear.monthNumber;
q.param(Income_.calendarYear).value = widget.monthYear.year;
return q.find();
}));
});
}
#override
void dispose() {
_listController.close();
super.dispose();
}
this is how i call the stream controller
Widget incomeSummaryStream() {
return StreamBuilder<List<Income>>(
stream: _listController.stream,
builder: (context, snapshot) {
final activeIncomes = snapshot.data;
if (!snapshot.hasData || activeIncomes!.length == 0) {
return Align(
alignment: Alignment.center,
child: noData()
);
}
return itemList(activeIncomes);
},
);
}
The app works as follow. here is my main screen with has a menu
when i clicked on Income Budget on the menu of the main screen, it goes to another screen which display a message since there is no data in the database.
so far this is good and there is no error. when I click on the menu of the top left corner and click on Home Screen link, it takes me back to the home screen but i get an error in android studio
Bad state: You cannot close the subject while items are being added from addStream
When the exception was thrown, this was the stack:
#0 Subject.close (package:rxdart/src/subjects/subject.dart:146:7)
#1 IncomeTransactionState.dispose (package:finsec/features/income/presentation/pages/income_transactions.dart:75:21)
#2 StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4818:11)
#3 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1883:13)
#4 ListIterable.forEach (dart:_internal/iterable.dart:39:13)
#5 _InactiveElements._unmountAll (package:flutter/src/widgets/framework.dart:1892:25)
#6 BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2837:27)
#7 BuildOwner.lockState (package:flutter/src/widgets/framework.dart:2468:15)
#8 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2836:7)
#9 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:884:19)
#10 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#11 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)
#12 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#13 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#17 _invoke (dart:ui/hooks.dart:150:10)
#18 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#19 _drawFrame (dart:ui/hooks.dart:114:31)
(elided 3 frames from dart:async)
this error is pointing to the following line in dispose() function
_listController.close();
if i remove the function dispose() and run the application and do the same steps as stated above, i get a different error
The following StateError was thrown building StreamBuilder<dynamic>(state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#fa4d5):
Bad state: Stream has already been listened to.
The relevant error-causing widget was:
StreamBuilder<dynamic> StreamBuilder:file:///C:/Users/Tato/Desktop/finsec/lib/main.dart:180:19
When the exception was thrown, this was the stack:
#6 Subject.addStream (package:rxdart/src/subjects/subject.dart:105:12)
#7 IncomeTransactionState.initState.<anonymous closure> (package:finsec/features/income/presentation/pages/income_transactions.dart:63:23)
#8 State.setState (package:flutter/src/widgets/framework.dart:1088:30)
#9 IncomeTransactionState.initState (package:finsec/features/income/presentation/pages/income_transactions.dart:51:5)
#10 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4728:57)
#11 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4561:5)
#12 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#13 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:20)
#14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#15 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#16 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#17 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#18 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#19 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#20 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#21 StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#22 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#24 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#25 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#26 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#27 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#28 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#29 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#30 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#31 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5626:32)
#32 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6284:17)
#33 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#34 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#35 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#36 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#37 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#38 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#40 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#41 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#42 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#43 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#44 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#45 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#46 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#47 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#48 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#49 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#50 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#51 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#52 StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#53 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#54 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#55 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#56 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#57 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#58 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#59 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#60 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#61 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#62 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#63 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#64 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#65 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#66 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#67 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#68 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#69 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#71 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#72 StatelessElement.update (package:flutter/src/widgets/framework.dart:4669:5)
#73 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#74 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#75 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#76 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#77 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#78 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#79 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#80 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#81 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#82 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#83 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#84 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#85 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#86 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#87 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#88 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#89 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#90 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#91 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2578:33)
#92 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#93 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#94 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)
#95 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#96 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#100 _invoke (dart:ui/hooks.dart:150:10)
#101 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#102 _drawFrame (dart:ui/hooks.dart:114:31)
(elided 9 frames from dart:async)
this error points to setState(() in void initState() function.
can someone help me and let me know how to change my code so that it works without errors? I want to be able to pass parameters to IncomeTransactions class so that they can be used in the following stream
_listController.addStream(objectBox.incomeQueryStream.map((q) {
q.param(Income_.monthNumber).value = widget.monthYear.monthNumber;
q.param(Income_.calendarYear).value = widget.monthYear.year;
the stream should display the data based on the parameters passed to the query. if i go to another screen, the stream should not give me those errors mentioned above.
i tried changing broadcast stream controller to this
StreamController<List<Income>> _listController = new BehaviorSubject();
but it didnt work either. please help. thanks in advance
Related
I am writing a program with a simple page scroller that changes pages when scrolled. I want to have these pages changed this way such that I can be able to change them with another widget that will be inserted into the stack. Currently, I am not fully understanding the error and would like to know why the error is thrown. The error is shown below. Each screen page item has only a Scaffold with text inside body.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../screens/account.dart';
import '../screens/home.dart';
import '../screens/post.dart';
import '../screens/saved.dart';
class ScreenChanger extends StatefulWidget {
const ScreenChanger({Key? key}) : super(key: key);
#override
State<ScreenChanger> createState() => _ScreenChangerState();
}
class _ScreenChangerState extends State<ScreenChanger> {
late HomeScreen homeScreen;
late SavedScreen savedScreen;
late PostScreen postScreen;
late AccountScreen accountScreen;
#override
void initState() {
homeScreen=HomeScreen();
savedScreen=SavedScreen();
postScreen=PostScreen();
accountScreen=AccountScreen();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ChangeNotifierProvider(
create: (_) { return ScreenController(); },
builder: (context, child) {
return Stack(
children: [
PageView.builder(
itemCount: 4,
itemBuilder: (context, index) {
setState(() {
Provider.of<ScreenController>(context, listen: false).currentPage=index;
});
return [
homeScreen,
savedScreen,
postScreen,
accountScreen,
][Provider.of<ScreenController>(context).currentPage];
},
),
],
);
},
),
);
}
}
class ScreenController extends ChangeNotifier{
late int _currentPage;
int get currentPage => _currentPage;
set currentPage(int value) {
_currentPage = value;
notifyListeners();
}
ScreenController({
int currentPage = 0
}){
this.currentPage=currentPage;
}
}
Error
======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for ScreenController:
setState() or markNeedsBuild() called during build.
This _InheritedProviderScope<ScreenController?> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<ScreenController?>
value: Instance of 'ScreenController'
listening to value
The widget which was currently being built when the offending call was made was: SliverFillViewport
When the exception was thrown, this was the stack:
#0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4549:11)
#1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4564:6)
#2 _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:577:5)
#3 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:351:24)
#4 ScreenController.currentPage= (package:real_estate_v02/widgets/screen_changer.dart:69:5)
#5 _ScreenChangerState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:45:75)
#6 State.setState (package:flutter/src/widgets/framework.dart:1114:30)
#7 _ScreenChangerState.build.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:44:19)
#8 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:485:22)
#9 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1376:28)
#10 SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1306:67)
#11 Iterable.forEach (dart:core/iterable.dart:325:35)
#12 SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:1353:24)
#13 SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:1282:7)
#14 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#15 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#16 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#18 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#19 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#21 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#22 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#23 _ViewportElement.update (package:flutter/src/widgets/viewport.dart:237:11)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#25 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#26 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#27 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#28 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#29 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#30 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#31 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#32 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#33 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#34 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#35 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#36 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#37 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#38 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#39 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#40 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#41 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#42 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#43 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#44 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#45 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#46 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#47 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#48 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#49 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#50 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#51 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#52 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#53 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#54 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#55 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#56 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#57 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#58 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#59 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#60 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#61 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#62 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#63 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#64 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#65 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#66 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#67 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#68 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#69 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#71 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#72 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#73 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#74 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#75 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#76 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#77 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#78 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#79 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#80 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#81 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#83 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#84 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#85 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#86 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#87 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#88 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#89 _InheritedProviderScopeElement.update (package:provider/src/inherited_provider.dart:523:11)
#90 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#91 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#92 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#93 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#94 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#96 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#97 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#98 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#100 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
.......
#350 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#351 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:881:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
The ScreenController sending notification was: Instance of 'ScreenController'
====================================================================================================
======== Exception caught by widgets library =======================================================
The following assertion was thrown building:
setState() or markNeedsBuild() called during build.
This ScreenChanger widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: ScreenChanger
state: _ScreenChangerState#ffffa
The widget which was currently being built when the offending call was made was: SliverFillViewport
When the exception was thrown, this was the stack:
#0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4549:11)
#1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4564:6)
#2 State.setState (package:flutter/src/widgets/framework.dart:1134:15)
#3 _ScreenChangerState.build.<anonymous closure>.<anonymous closure> (package:real_estate_v02/widgets/screen_changer.dart:44:19)
#4 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:485:22)
#5 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1376:28)
#6 SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1306:67)
#7 Iterable.forEach (dart:core/iterable.dart:325:35)
#8 SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:1353:24)
#9 SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:1282:7)
#10 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#11 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#12 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#13 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#14 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#15 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#16 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#17 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#18 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#19 _ViewportElement.update (package:flutter/src/widgets/viewport.dart:237:11)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#21 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#22 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#23 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#25 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#26 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#27 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#28 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#30 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#31 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#32 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#33 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#34 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#35 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#37 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#38 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#39 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#40 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#41 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#42 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#43 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#44 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#45 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#46 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#47 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#48 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#49 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#50 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6307:14)
#51 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#52 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#53 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#54 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#55 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#56 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#57 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#58 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#59 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#60 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#61 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#62 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#63 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#64 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#65 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#66 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#67 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#68 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#69 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#71 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#72 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#73 StatefulElement.update (package:flutter/src/widgets/framework.dart:5082:5)
#74 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#75 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5904:32)
#76 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6460:17)
#77 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#78 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#79 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#80 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#81 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#83 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#84 ProxyElement.update (package:flutter/src/widgets/framework.dart:5228:5)
#85 _InheritedProviderScopeElement.update (package:provider/src/inherited_provider.dart:523:11)
#86 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#87 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#88 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#89 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#90 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#91 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#92 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#93 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#94 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#96 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#97 StatelessElement.update (package:flutter/src/widgets/framework.dart:4956:5)
#98 Element.updateChild (package:flutter/src/widgets/framework.dart:3570:15)
#99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
#100 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
.....
#345 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
#346 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#347 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:881:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
====================================================================================================
Found another solution:
Widget build(BuildContext context) {
return Scaffold(
body: ChangeNotifierProvider(
create: (_) { return ScreenController(); },
builder: (context, child) {
final screenController = Provider.of<ScreenController>(context);
return Stack(
children: [
PageView.builder(
itemCount: 4,
itemBuilder: (context, index) {
setState(() {
screenController.currentPage=index;
});
return [
homeScreen,
savedScreen,
postScreen,
accountScreen,
][screenController.currentPage];
},
),
],
);
},
),
);
}
You are calling setState middle of widget build. You can wait to finish the frame and then call setState.
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
Provider.of<ScreenController>(context, listen: false).currentPage = index;
}); // you may not needed using setState because there is not state variable needed to be updated
I am building a Flutter application -using Navigator 2.0 with go_router- with nested navigation on the bottom navigation bar, and 3 sub-route on the Home route: Single Post, Single Category, and Single Tag.
When I tried to open a single post URL on the browser, the deep link worked, and it opened the Single Post page on the app. But then, when I clicked the page's back button or the android back-gesture, it has thrown a _CastError: Null check operator used on a null value, and the Home page is not displayed.
What's wrong here, and how can I solve this?
Here is the source code: https://github.com/KweeksNews/kweeksnews_app/tree/develop (branch develop)
Error is thrown on this line: https://github.com/KweeksNews/kweeksnews_app/blob/11747545ab09dfc829ce02094ad16f9735cc39be/lib/core/widgets/navbar.dart#L68
Here is the debug log:
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown, building Builder:
Null check operator used on a null value
The relevant error-causing widget was
MaterialApp
When the exception was thrown, this was the stack
#0 ScrollPosition.viewportDimension
#1 _PagePosition.getPixelsFromPage
#2 PageController.jumpToPage
#3 _NavBarState.didUpdateWidget
#4 StatefulElement.update
#5 Element.updateChild
#6 SingleChildRenderObjectElement.update
#7 Element.updateChild
#8 ComponentElement.performRebuild
#9 Element.rebuild
#10 StatelessElement.update
#11 Element.updateChild
#12 SingleChildRenderObjectElement.update
#13 Element.updateChild
#14 SingleChildRenderObjectElement.update
#15 Element.updateChild
#16 ComponentElement.performRebuild
#17 StatefulElement.performRebuild
#18 Element.rebuild
#19 StatefulElement.update
#20 Element.updateChild
#21 SingleChildRenderObjectElement.update
#22 Element.updateChild
#23 SingleChildRenderObjectElement.update
#24 Element.updateChild
#25 ComponentElement.performRebuild
#26 StatefulElement.performRebuild
#27 Element.rebuild
#28 StatefulElement.update
#29 Element.updateChild
#30 ComponentElement.performRebuild
#31 Element.rebuild
#32 StatelessElement.update
#33 Element.updateChild
#34 ComponentElement.performRebuild
#35 StatefulElement.performRebuild
#36 Element.rebuild
#37 StatefulElement.update
#38 Element.updateChild
#39 SingleChildRenderObjectElement.update
#40 Element.updateChild
#41 SingleChildRenderObjectElement.update
#42 Element.updateChild
#43 ComponentElement.performRebuild
#44 Element.rebuild
#45 ProxyElement.update
#46 _InheritedNotifierElement.update
#47 Element.updateChild
#48 SingleChildRenderObjectElement.update
#49 Element.updateChild
#50 ComponentElement.performRebuild
#51 StatefulElement.performRebuild
#52 Element.rebuild
#53 StatefulElement.update
#54 Element.updateChild
#55 ComponentElement.performRebuild
#56 Element.rebuild
#57 ProxyElement.update
#58 Element.updateChild
#59 ComponentElement.performRebuild
#60 Element.rebuild
#61 ProxyElement.update
#62 Element.updateChild
#63 ComponentElement.performRebuild
#64 StatefulElement.performRebuild
#65 Element.rebuild
#66 StatefulElement.update
#67 Element.updateChild
#68 ComponentElement.performRebuild
#69 Element.rebuild
#70 StatelessElement.update
#71 Element.updateChild
#72 ComponentElement.performRebuild
#73 Element.rebuild
#74 StatelessElement.update
#75 Element.updateChild
#76 SingleChildRenderObjectElement.update
#77 Element.updateChild
#78 ComponentElement.performRebuild
#79 Element.rebuild
#80 ProxyElement.update
#81 Element.updateChild
#82 ComponentElement.performRebuild
#83 Element.rebuild
#84 ProxyElement.update
#85 Element.updateChild
#86 ComponentElement.performRebuild
#87 StatefulElement.performRebuild
#88 Element.rebuild
#89 StatefulElement.update
#90 Element.updateChild
#91 ComponentElement.performRebuild
#92 StatefulElement.performRebuild
#93 Element.rebuild
#94 StatefulElement.update
#95 Element.updateChild
#96 ComponentElement.performRebuild
#97 StatefulElement.performRebuild
#98 Element.rebuild
#99 BuildOwner.buildScope
#100 WidgetsBinding.drawFrame
#101 RendererBinding._handlePersistentFrameCallback
#102 SchedulerBinding._invokeFrameCallback
#103 SchedulerBinding.handleDrawFrame
#104 SchedulerBinding._handleDrawFrame
#108 _invoke (dart:ui/hooks.dart:151:10)
#109 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#110 _drawFrame (dart:ui/hooks.dart:115:31)
I'm not pretty sure about the real cause, but this is what I think happened.
So when the app opened using deep link into SingePost page, the app puts a page containing Navbar widget first into the page stack, and then puts the SinglePost page on top of it. Here it should be noted that the Navbar is not yet rendered by the app.
The Navbar widget's body is a PageView containing 4 page: Home, Search, SavedPosts, and Settings. All four pages are implementing AutomaticKeepAliveClientMixin to save and keep the scroll position, so that when user is navigating to the other pages and return, the scroll position is persist.
When user is popping out from the SinglePost page, the app then will render the Navbar with Home as the PageView's selected child (because the Navbar is given with index parameter of 0). Now the interesting and confusing part is, the didUpdateWidget is also called when the Navbar is being rendered by the app after user popping out from SinglePost page, even no parameter is changed (the index still 0). This will trigger a _pageController.jumpToPage() function, which, I think, in turn will try to render the requested page with the last saved scroll position. But because the page has never been rendered before, the scroll position is Null.
To fix this, now I put a check to make sure the new index is different with the current index before calling the _pageController.jumpToPage() function, and it worked.
I know there are a lot of similar questions already out there, but none of the answers seem to resolve my problem.
I get the error Null check operator used on a null value, however I don't use the bang operator anywhere in my code.
Here's my main.dart file:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:todo/screens/home.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const App());
}
class App extends StatefulWidget {
const App({Key? key}) : super(key: key);
#override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp( // ERROR CAUSING WIDGET
routes: {
'/': (context) => const HomeScreen()
},
// home: const HomeScreen(), NO ERROR!
);
}
},
);
}
}
The home.dart file looks like this:
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('HOME'),
),
);
}
}
The problem only occurs when using routes, when I assign HomeScreen() directly to the home property of MaterialApp, everything works fine.
Also note that everything worked fine before I added Firebase and the FutureBuilder.
Edit: I shortened the above code for better readability. I actually use the FutureBuilder to differentiate between multiple screens (a loading screen, an error screen and the home screen), depending on the future's state.
Here's the full stacktrace:
The relevant error-causing widget was:
MaterialApp MaterialApp:file:///D:/Code/Workspace/Flutter/todo/lib/main.dart:43:18
When the exception was thrown, this was the stack:
#0 _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1350:48)
#1 MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:53:55)
#2 MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:106:27)
#3 _ModalScopeState.build.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/routes.dart:872:55)
#4 Builder.build (package:flutter/src/widgets/basic.dart:7398:48)
#5 StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#8 StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#9 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#10 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#12 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#13 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#15 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#16 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#17 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#18 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#19 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#21 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#22 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#24 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#25 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#26 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#27 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#29 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#30 StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#31 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#32 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#33 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#34 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#35 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#36 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#37 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#38 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#39 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#40 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#42 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#43 ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#44 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:111:11)
#45 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#46 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#47 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#48 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#49 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#50 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#51 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#52 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#54 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#55 ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#56 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#57 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#58 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#59 ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#60 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#61 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#62 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#63 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#64 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#65 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#66 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#67 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#68 StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#69 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#71 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#72 StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#73 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#74 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#75 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#76 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#77 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#78 ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#79 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#80 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#81 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#82 ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#83 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#84 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#85 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#86 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#87 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#88 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#89 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#90 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#91 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#92 StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#93 Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#94 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#95 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#96 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#97 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#98 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#99 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#100 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#101 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#102 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
#106 _invoke (dart:ui/hooks.dart:151:10)
#107 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#108 _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
If you are using the future just for firebase initialization, you can simply try this one.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
When you use Named Routes in your Flutter app you need to provide the "initialRoute" attribute for the MaterialApp widget.
In your case add the following line just before the "routes" attribute:
initialRoute: '/',
so your MaterialApp code will look like this:
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => const HomeScreen()
},
);
Note: You must remove the "home" attribute of MaterialApp as you cannot have both the "home" and "initialRoute" attributes being set at the same time.
So I get the following error whenever I sign in through Google, but I only get it on my first login. If I log out and log back in, it never appears and the app works fine.
======== Exception caught by widgets library =======================================================
The following StateError was thrown building:
Bad state: field does not exist within the DocumentSnapshotPlatform
When the exception was thrown, this was the stack:
#0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:87:7)
#1 DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:105:23)
#2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:121:12)
#3 _JsonDocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:92:48)
#4 _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40)
#5 _HomeState.chatRoomsList.<anonymous closure>.<anonymous closure> (package:connectlyapp/views/home.dart:58:45)
#6 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:456:22)
#7 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1214:28)
#8 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1227:55)
#9 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2573:19)
#10 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1220:12)
#11 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:349:23)
#12 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1968:59)
#13 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:916:15)
#14 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1968:14)
#15 RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:338:5)
#16 RenderSliverMultiBoxAdaptor.addInitialChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:422:5)
#17 RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:79:12)
#18 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#19 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:137:12)
#20 RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:371:11)
#21 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#22 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:510:13)
#23 RenderShrinkWrappingViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1923:12)
#24 RenderShrinkWrappingViewport.performLayout (package:flutter/src/rendering/viewport.dart:1881:20)
#25 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#26 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#27 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#28 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#29 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#30 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#31 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#32 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#33 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#34 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#35 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#36 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#37 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#38 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#39 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#40 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#41 RenderCustomPaint.performLayout (package:flutter/src/rendering/custom_paint.dart:545:11)
#42 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#43 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#44 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#45 ChildLayoutHelper.layoutChild (package:flutter/src/rendering/layout_helper.dart:56:11)
#46 RenderFlex._computeSizes (package:flutter/src/rendering/flex.dart:829:43)
#47 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:931:32)
#48 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#49 _RenderSingleChildViewport.performLayout (package:flutter/src/widgets/single_child_scroll_view.dart:581:14)
#50 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#51 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#52 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#53 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#54 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#55 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#56 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#57 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#58 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#59 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#60 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#61 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#62 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#63 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#64 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#65 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#66 RenderCustomPaint.performLayout (package:flutter/src/rendering/custom_paint.dart:545:11)
#67 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#68 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:116:14)
#69 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#70 RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:233:12)
#71 RenderObject.layout (package:flutter/src/rendering/object.dart:1858:7)
#72 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:171:12)
#73 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:1097:7)
#74 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:240:7)
#75 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:404:14)
#76 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1713:7)
#77 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:885:18)
#78 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:453:19)
#79 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:883:13)
#80 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
#81 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1143:15)
#82 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1080:9)
#83 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#87 _invoke (dart:ui/hooks.dart:166:10)
#88 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#89 _drawFrame (dart:ui/hooks.dart:129:31)
(elided 3 frames from dart:async)
====================================================================================================
This is the code that the error pops up on:
Widget chatRoomsList() {
return StreamBuilder<dynamic>(
stream: chatRoomsStream,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data.docs.length,
shrinkWrap: true,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.docs[index];
return ChatRoomListTile(ds["lastMessage"], ds.id, myUserName);
})
: Center(child: CircularProgressIndicator());
},
);
}
What this snippet of code does is fetch chatrooms that the logged in user has interacted with, on the main page. Sort of like a Messenger Home screen. But if a new user logs in (who has no chatrooms or existing chats with any other user), the main screen should show no results.
What am I doing wrong here? I had a similar problem with my search feature on the same app, and I fixed it by putting in return Streambuilder, but here - that doesn't fix the Bad state error.
The main screen works fine if I log out and log back in, it displays the users I chatted with and their previous/last sent messages.
Kindly help me out here.
EDIT: getChatRooms() [where chatRoomsStream is initialized]:
getChatRooms() async {
chatRoomsStream = await DatabaseMethods().getChatRooms();
setState(() {});
}
DatabaseMethods().getChatRooms() [database.dart method]:
Future<Stream<QuerySnapshot>> getChatRooms() async {
String myUsername = await SharedPreferenceHelper().getUserName();
return FirebaseFirestore.instance
.collection("chatrooms")
.where("users", arrayContains: myUsername)
.snapshots();
}
I believe the issue is in this line
return ChatRoomListTile(ds["lastMessage"], ds.id, myUserName);
try changing this line to ds.data()['lastMessage']
return ChatRoomListTile(ds.data()['lastMessage'], ds.id, myUserName);
Code that worked for me
final usersRef = FirebaseFirestore.instance.collection('users');
final userDocs = await usersRef.get();
final docId =
userDocs.docs.where((doc) => doc.data()['uid'] == user.uid).first.id;
Please lemme know in comments if you any issues, will be glad to help you.
You should use doc.data() but once you use it you will get an error saying operator[] isn't defined for the type Object. That's because in recent version of firestore Document is a generic type and you must also give a type along with it example
DocumentSnapshot<Map<String,dynamic>>
if you don't give any type like this - DocumentSnapshot .The type is inferred as an Object type that doesn't suppose to have "[]" operator defined.
Please look at these two answers for more details-
https://stackoverflow.com/a/63501611/6051666
https://stackoverflow.com/a/69447502/6051666
I have a Timer that has a small animation which is a circle around it. The Timer is called from another route to start (ExamTimer(size: 40.0),). And when ever I navigate to another route I get the following Error:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
ExamTimerState#c1b45(tickers: tracking 1 ticker) was disposed with an active Ticker.
ExamTimerState created a Ticker via its TickerProviderStateMixin, but at the time dispose() was called on the mixin, that Ticker was still active. All Tickers must be disposed before calling super.dispose().
Tickers used by AnimationControllers should be disposed by calling dispose() on the AnimationController itself. Otherwise, the ticker will leak.
The offending ticker was: _WidgetTicker(created by ExamTimerState#c1b45(lifecycle state: created, tickers: tracking 0 tickers))
The stack trace when the _WidgetTicker was actually created was:
#0 new Ticker.<anonymous closure> (package:flutter/src/scheduler/ticker.dart:66:40)
#1 new Ticker (package:flutter/src/scheduler/ticker.dart:68:6)
#2 new _WidgetTicker (package:flutter/src/widgets/ticker_provider.dart:237:80)
#3 TickerProviderStateMixin.createTicker (package:flutter/src/widgets/ticker_provider.dart:168:34)
#4 new AnimationController (package:flutter/src/animation/animation_controller.dart:245:21)
#5 ExamTimerState.initState (package:azmoonasli/models/ExamTimer.dart:27:18)
#6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
#7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#9 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
#10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#13 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#14 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#15 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#16 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#18 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#19 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#20 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#21 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#22 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#28 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#30 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#33 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#36 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#38 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#40 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#41 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#42 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
#43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#44 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#45 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#47 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#48 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#49 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#50 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
#51 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#52 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#53 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#54 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#55 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#56 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#58 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#59 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#60 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#61 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#62 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#63 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#64 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#65 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#66 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#67 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#68 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#69 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#70 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#72 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#73 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#74 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#75 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#76 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#77 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#78 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#79 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#80 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#81 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4617:11)
#82 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#83 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
#84 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#85 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#86 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#87 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#88 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#89 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#90 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#91 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#92 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#93 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#94 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#95 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#96 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#97 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#98 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#100 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#101 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#102 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#103 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#104 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#105 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#106 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#108 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#109 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#110 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#111 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#112 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#113 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#114 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#115 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#116 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#117 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#118 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#119 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#120 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#121 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#122 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#123 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#124 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#125 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#126 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#127 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#128 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#129 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#130 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#131 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#132 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#133 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#134 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#135 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#136 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#137 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#138 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#139 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#140 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#141 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#142 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#143 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#144 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#145 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#146 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#147 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#148 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#149 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#150 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#151 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#152 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#153 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#154 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#155 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#156 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#157 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#158 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#159 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#160 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#161 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#162 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#163 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#164 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#165 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#166 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#167 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#168 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#169 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#170 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#171 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#172 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#173 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#174 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#175 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#176 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#177 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#178 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#179 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#180 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#181 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#182 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#183 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#184 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#185 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#186 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#187 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#188 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#189 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#190 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#191 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#192 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#193 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#194 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#195 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#196 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#197 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#198 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#199 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#200 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#201 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#202 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#203 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
#204 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#205 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#206 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#207 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#208 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#209 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#210 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#211 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#212 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#213 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#217 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#224 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#225 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#226 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#227 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#228 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#229 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#230 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#231 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#232 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#233 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#234 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#235 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#236 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#237 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
#238 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
#239 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#240 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#241 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
#242 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5219:32)
#243 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5561:17)
#244 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#245 _TheatreElement.update (package:flutter/src/widgets/overlay.dart:605:16)
#246 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#247 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#249 StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
#250 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#251 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#252 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#253 ProxyElement.update (package:flutter/src/widgets/framework.dart:4557:5)
#254 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:94:11)
#255 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#256 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
#257 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#258 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#259 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#260 StatefulElement.update (package:flutter/src/widgets/framework.dart:4413:5)
#261 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#262 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
#263 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#264 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
#265 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#266 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#267 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
#268 StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5)
#269 Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
#270 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
#272 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)
#273 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)
#274 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
#275 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
#276 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
#277 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
#281 _invoke (dart:ui/hooks.dart:259:10)
#282 _drawFrame (dart:ui/hooks.dart:217:3)
(elided 3 frames from package dart:async)
When the exception was thrown, this was the stack:
#0 TickerProviderStateMixin.dispose.<anonymous closure> (package:flutter/src/widgets/ticker_provider.dart:185:13)
#1 TickerProviderStateMixin.dispose (package:flutter/src/widgets/ticker_provider.dart:203:6)
#2 StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4435:12)
#3 _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1748:13)
#4 _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1746:7)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Here's my code:
import 'package:flutter/material.dart';
import 'dart:math' as math;
class ExamTimer extends StatefulWidget {
final double size;
ExamTimer({#required this.size, });
#override
ExamTimerState createState() => ExamTimerState();
}
class ExamTimerState extends State<ExamTimer> with TickerProviderStateMixin {
AnimationController controller;
String get timerString {
Duration duration = controller.duration * controller.value;
return '${duration.inMinutes}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
}
#override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(minutes: 20),
);
}
#override
Widget build(BuildContext context) {
ThemeData themeData = Theme.of(context);
controller.reverse(from: controller.value == 0.0 ? 1.0 : controller.value);
return Stack(
alignment: Alignment.center,
children: <Widget>[ AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child) {
return CustomPaint(
size: Size(widget.size, widget.size),
painter: TimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
));
},
),
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child) {
return Text(
timerString,
style: TextStyle(fontSize: 12.0),
);
}),
],
);
}
}
class TimerPainter extends CustomPainter {
TimerPainter({
this.animation,
this.backgroundColor,
this.color,
}) : super(repaint: animation);
final Animation<double> animation;
final Color backgroundColor, color;
#override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, progress, false, paint);
}
#override
bool shouldRepaint(TimerPainter old) {
return animation.value != old.animation.value ||
color != old.color ||
backgroundColor != old.backgroundColor;
}
}
So I have 3 questions:
How to dispose() the timer when user manually Navigates to another route?
I need to know The time from when user started ExamTimer() to when he navigated to another route(Total time passed).
How to Navigate automatically to another route when the timer finishes it's duration.
A sample code or any approach would be so appreciated.
First, You need to dispose your AnimationController by this way
controller?.dispose();
About your questions:
1. This is how you can dispose a Timer
yourTimer.cancel();
2.Eleapsed time can be calculated ether by using StopWatch or Timer
StopWatch
//start a stopwatch
final watch = StopWatch()..start();
//Eleapsed time
watch.eleapsed //returns a Duration
//Stop it
watch.stop()
//You can also reset when you return to your page
watch.reset();
Timer
//Start
final timer = Timer.periodic(Duration(seconds:1), (Timer ticker){
if(ticker.tick == 2){//Wait for 1*2 seconds
//Navigate here
}
//Total eleapsed time
timer.tick*yourDurationUnit // used when creating this timer
)
Navigate automatically
Timer timer = Timer(Duration(seconds: 2), (){
//Navigate here
});
NOTE: You should dispose Timer and StopWatch in dispose method or it'll cause memory leaks