return 类型 'Null' 不是闭包上下文所要求的 'Widget'
The return type 'Null' isn't a 'Widget', as required by the closure's context
迁移到 flutter null-safety 后我遇到了一些问题:
return 类型 'Null' 不是闭包上下文所要求的 'Widget'
参数类型'Null'无法赋值给参数类型'AuthenticationRepository'
参数类型'Null'无法赋值给参数类型'DatabaseRepository'
参数类型'Null'无法赋值给参数类型'RemoteConfig'
class _PlacesState extends State<_Places> {
bool _isLoading = false;
final TextEditingController _typeAheadController = TextEditingController();
@override
Widget build(BuildContext context) {
return TypeAheadFormField<Map<String, dynamic>>(
key: widget.formFieldKey,
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
decoration: InputDecoration(
hintText: 'Place',
contentPadding: EdgeInsets.only(left: 8, right: 8),
border: InputBorder.none,
),
controller: _typeAheadController,
),
suggestionsCallback: (String? pattern) async {
final s = pattern?.trim();
if (s == null || s == '' || s.length < 4) return null;
_isLoading = true;
try {
await Future.delayed(Duration(seconds: 1));
final data = await (_request(s) as FutureOr<Map<String, dynamic>>);
final result = data['hits'] as List;
return result.cast<Map<String, dynamic>>();
} finally {
_isLoading = false;
}
} as FutureOr<Iterable<Map<String, dynamic>>> Function(String),
loadingBuilder: (BuildContext context) {
if (!_isLoading) return null; **<-- Error is here**
return Align(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: ExtendedProgressIndicator(),
),
);
},
--
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(App(
authenticationRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'AuthenticationRepository'
databaseRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'DatabaseRepository'
remoteConfig: null, <-- The argument type 'Null' can't be assigned to the parameter type 'RemoteConfig'
));
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
在您的 App 小部件中,放置可为 null 的存储库。
final AuthenticationRepository? authenticationRepository
或者模拟您的存储库并将其提供给 App 而不是提供 null。
迁移到 flutter null-safety 后我遇到了一些问题:
return 类型 'Null' 不是闭包上下文所要求的 'Widget'
参数类型'Null'无法赋值给参数类型'AuthenticationRepository'
参数类型'Null'无法赋值给参数类型'DatabaseRepository'
参数类型'Null'无法赋值给参数类型'RemoteConfig'
class _PlacesState extends State<_Places> {
bool _isLoading = false;
final TextEditingController _typeAheadController = TextEditingController();
@override
Widget build(BuildContext context) {
return TypeAheadFormField<Map<String, dynamic>>(
key: widget.formFieldKey,
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
decoration: InputDecoration(
hintText: 'Place',
contentPadding: EdgeInsets.only(left: 8, right: 8),
border: InputBorder.none,
),
controller: _typeAheadController,
),
suggestionsCallback: (String? pattern) async {
final s = pattern?.trim();
if (s == null || s == '' || s.length < 4) return null;
_isLoading = true;
try {
await Future.delayed(Duration(seconds: 1));
final data = await (_request(s) as FutureOr<Map<String, dynamic>>);
final result = data['hits'] as List;
return result.cast<Map<String, dynamic>>();
} finally {
_isLoading = false;
}
} as FutureOr<Iterable<Map<String, dynamic>>> Function(String),
loadingBuilder: (BuildContext context) {
if (!_isLoading) return null; **<-- Error is here**
return Align(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: ExtendedProgressIndicator(),
),
);
},
--
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(App(
authenticationRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'AuthenticationRepository'
databaseRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'DatabaseRepository'
remoteConfig: null, <-- The argument type 'Null' can't be assigned to the parameter type 'RemoteConfig'
));
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
在您的 App 小部件中,放置可为 null 的存储库。
final AuthenticationRepository? authenticationRepository
或者模拟您的存储库并将其提供给 App 而不是提供 null。