- The following code will make an error, and the return value is Widget? instead of Widget. I want to be able to force Widget? to Widget, but how to do it?
/// Get the parent widget in the subtree
class ContextRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Context test"),
),
body: Container(
child: Builder(builder: (context) {
// Find the nearest parent up in the Widget tree `Scaffold` widget
Scaffold? scaffold = context.findAncestorWidgetOfExactType<Scaffold>();
// Return the title of AppBar directly, here is actually Text ("Context test")
Widget? widget1 = (scaffold!.appBar as AppBar).title;
return widget1;
}),
),
);
}
}