【发布时间】:2021-09-03 23:18:20
【问题描述】:
- 以下代码会报错,返回值为Widget?而不是小部件。我希望能够强制 Widget?到 Widget,但是怎么做呢?
/// 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;
}),
),
);
}
}
【问题讨论】:
标签: flutter dart widget dart-null-safety