My MainView contains a TabBarView. An error is thrown if I
- navigate to another page
- hot reload
- navigate back
This is the code of my MainView:
import 'package:flutter/material.dart';
import '../constants/data.dart';
import '../services/data/data_service.dart';
import 'nutvalues_view.dart';
class MainView extends StatefulWidget {
const MainView({super.key});
@override
State<MainView> createState() => _MainViewState();
}
class _MainViewState extends State<MainView> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return ScrollConfiguration(
// clamping scroll physics to avoid overscroll
behavior: const ScrollBehavior().copyWith(overscroll: false),
child: DefaultTabController(
length: 1,
child: Scaffold(
appBar: PreferredSize(
preferredSize: _tabBar.preferredSize,
child: Material(
color: Theme.of(context).appBarTheme.backgroundColor,
child: SafeArea(child: _tabBar),
)
),
body: TabBarView( // <------- THIS LINE CAUSES THE ERROR
physics: AlwaysScrollableScrollPhysics(),
children: const [
NutritionalValueView(),
],
),
)
),
);
}
TabBar get _tabBar => const TabBar(
isScrollable: true,
physics: AlwaysScrollableScrollPhysics(),
tabs: [
Tab(text: "Nutrition"),
],
);
}
This is the error thrown:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following TypeErrorImpl was thrown while rebuilding dirty elements:
Unexpected null value.
The relevant error-causing widget was:
TabBarView TabBarView:file:///D:/Workspace/food_tracker/lib/views/main_view.dart:46:17
When the exception was thrown, this was the stack:
dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3 throw
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 586:18 nullCheck
packages/flutter/src/widgets/scroll_position.dart 260:53 get viewportDimension
packages/flutter/src/widgets/page_view.dart 381:19 getPixelsFromPage
packages/flutter/src/widgets/page_view.dart 215:29 jumpToPage
packages/flutter/src/material/tabs.dart 1976:7 didChangeDependencies
packages/flutter/src/widgets/framework.dart 5647:7 performRebuild
packages/flutter/src/widgets/framework.dart 5203:7 rebuild
packages/flutter/src/widgets/framework.dart 2905:18 buildScope
packages/flutter/src/widgets/binding.dart 1136:9 drawFrame
packages/flutter/src/rendering/binding.dart 443:5 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1392:7 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1313:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1171:5 [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1404:5 invoke
lib/_engine/engine/platform_dispatcher.dart 307:5 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 187:36
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 426:37 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 431:39 dcall
The element being rebuilt at the time was index 10 of 18:
TabBarView
I don’t know which value could even be null. My apps works just fine besides the error printed to the console. NutritionalValueView
doesn’t seem to be to blame, as I’ve tried the same thing with other views too. I’m using Flutter 3.22.0 on Chrome.