Using Flutter, I am trying to pass data via the constructor to a new screen.
However, this is somewhat of a special case because the screen is a Stateful widget and I am using the Navigation Routes method of navigation.
The data also happens to be of type int, if that matters.
The named route navigation is set up like so:
void main() => runApp(Main());
class Main extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: PreLoadScreen.id,
//initialRoute: TemporaryScreen.id,
routes: {
TemporaryScreen.id: (context) => TemporaryScreen(),
InfoScreen.id: (context) => InfoScreen(),
PreLoadScreen.id: (context) => PreLoadScreen(),
StatsScreen.id: (context) => StatsScreen(),
RideScreen.id: (context) => RideScreen(),
AudioScreen.id: (context) => AudioScreen(),
},
);
}
}
The screen that I'm passing the data to has the following constructor code:
class StatsScreen extends StatefulWidget {
static const String id = 'stats_screen';
int tableID; // current shift table ID being passed in from super
// Constructor required for having data passed in
StatsScreen({Key key, @required this.tableID}) : super(key: key);
@override
_StatsScreenState createState() {
print('statsscreen DEBUG: $tableID'); // <-- this shows the data passed was NULL! :(
return _StatsScreenState();
}
}
The screen that I'm passing the data FROM contains the following code:
void _checkShiftStatus() async {
bool userClockedIn = await ShiftManager().isUserClockedIn();
int tableName = await ShiftManager().getActiveRideTableName();
print('preload DEBUG: tablename: $tableName'); // <-- this verifies the data is NOT null here.
if (userClockedIn) {
Navigator.pushNamed(context, StatsScreen.id,
arguments: {'tableID': tableName}); // <--- something is wrong here, presumably
} else {
shouldDisplayStartShift = !userClockedIn;
showProgressSpinner = false;
}
}
I've tried changing the suspect line to:
Navigator.pushNamed(context, StatsScreen.id,
arguments: tableName);
and...
Navigator.pushNamed(context, StatsScreen.id,
arguments: {tableName});
But get the same result in the target screen (data passed is null). It's sort of like baseball... the batter is the initial screen... and the catcher is the screen we're navigating to. The ball is the data. Except in my case, the batter seems to be Sammy Sosa and the ball is out of the park someplace... which is great for the Cubs but not for me.
I've also tried googling, docs, stackoverflow (even this specific answer... but I can't seem to extract the pertinent meaning from it), and Bacardi... and I'm getting very annoyed. Please someone point out my syntax error and what line it's on. Thank you!
You have to access data using ModalRoute.
class Delete2 extends StatefulWidget {
Delete2({Key key}) : super(key: key);
@override
_Delete2State createState() => _Delete2State();
}
class _Delete2State extends State<Delete2> {
@override
Widget build(BuildContext context) {
final int args = ModalRoute.of(context).settings.arguments;
return Container(
child: Text(args.toString()),
);
}
}