Reputation: 575
I'm currently testing how to implement a dashboard page where I make a page out of multiple StatefulWidgets to make maintaining fairly easy later on. I already know how to pass objects between StatefulWidgets, but I'm having an issue passing the same object to a StatefulWidget that is inside another StatefulWidget's build. I'm getting an error saying "Invalid constant value" on the object I passed to the child StatefulWidget. I've tried to change the child StatefulWidget's constructor like remove the required for the object parameter or making the constructor not constant, but it results in other kinds of error on that widget. Right now with my current code, I'm getting an error saying "Error: Undefined name 'widget'." on the ShippingDocPage(user: widget.user) line. Does the widget variable disappear for the Scaffold body?
Here's my code:
class DashboardPage extends StatefulWidget {
final User user;
const DashboardPage({super.key, required this.user});
@override
State<DashboardPage> createState() => _DashboardPageState();
}
class _DashboardPageState extends State<DashboardPage> {
var appBarTitleText = Text("Dashboard");
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: [
Tab(text: 'Tab1'),
Tab(text: 'Tab2'),
Tab(text: 'Tab3'),
],
),
title: Text(widget.user.userType!.displayName),//this works just fine
),
body: Column(
children: [
Container(
height: 150,
child: const Text('Vehicle Replace'),
),
Container(
height: 150,
child: const TabBarView(
children: [
ShippingDocPage(user: widget.user),//this is where the error shows
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
],
),
)
);
}
}
Then this the "child" StatefulWidget:
class ShippingDocPage extends StatefulWidget {
final User user;
const ShippingDocPage({super.key, required this.user});
@override
State<ShippingDocPage> createState() => _ShippingDocPageState();
}
class _ShippingDocPageState extends State<ShippingDocPage> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: Text('Shipping Documents'),
),
body: Column(
children: [
Container(
height: 150,
child: Text(widget.user.userType!.displayName),
),
],
),
)
);
}
}
Upvotes: 0
Views: 41
Reputation: 156
class DashboardPage extends StatefulWidget {
final User user;
const DashboardPage({super.key, required this.user});
@override
State<DashboardPage> createState() => _DashboardPageState();
}
class _DashboardPageState extends State<DashboardPage> {
var appBarTitleText = Text("Dashboard");
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: [
Tab(text: 'Tab1'),
Tab(text: 'Tab2'),
Tab(text: 'Tab3'),
],
),
title: Text(widget.user.userType!.displayName), // This works just fine
),
body: Column(
children: [
Container(
height: 150,
child: const Text('Vehicle Replace'),
),
Container(
height: 150,
child: TabBarView(
children: [
ShippingDocPage(user: widget.user), // Remove const here
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
],
),
),
);
}
}
class ShippingDocPage extends StatefulWidget {
final User user;
const ShippingDocPage({super.key, required this.user});
@override
State<ShippingDocPage> createState() => _ShippingDocPageState();
}
class _ShippingDocPageState extends State<ShippingDocPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shipping Documents'),
),
body: Column(
children: [
Container(
height: 150,
child: Text(widget.user.userType!.displayName),
),
],
),
);
}
}
Upvotes: 0