为什么 BlocProvider 使用 null-safety 获取这两个错误?
Why BlocProvider Gets this two errors with null-safety?
错误一:
error: 'Null' doesn't conform to the bound 'BlocBase<Object?>' of the type parameter 'T'. (type_argument_not_matching_bounds
错误 2:
error: Couldn't infer type parameter 'T'.
Tried to infer 'Null' for 'T' which doesn't work:
Type parameter 'T' is declared to extend 'BlocBase<Object?>' producing 'BlocBase<Object?>'.
The type 'Null' was inferred from:
Parameter 'create' declared as 'T Function(BuildContext)'
but argument is 'Null Function(BuildContext)'.
考虑将显式类型参数传递给泛型。
(could_not_infer 在 [g_movies] lib\layout\movies_layout.dart:12)
class MoviesLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) {
MoviesCubit()..getTopRatedData();
},
child: BlocConsumer<MoviesCubit, MoviesStates>(
listener: (context, state) {},
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(
'GMovies',
style: GoogleFonts.oswald(
fontWeight: FontWeight.bold,
fontSize: 24,
color: defaultColor,
),
),
actions: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: CircleAvatar(
radius: 20,
child: IconButton(
icon: Icon(Icons.person),
onPressed: () {},
),
),
),
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
ExpandablePanel(
theme: ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
),
header: Row(
children: [
Text(
'Top Rated',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
SizedBox(
width: 5,
),
Text(
'Movies',
style: TextStyle(
fontSize: 22,
),
),
],
),
collapsed: SizedBox(
height: 2,
),
expanded: SizedBox(
width: double.infinity,
height: 200,
child: ListView.separated(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => movieItem(),
separatorBuilder: (context, index) => SizedBox(
width: 10,
),
itemCount: 10,
),
),
),
SizedBox(
height: 10,
),
ExpandablePanel(
theme: ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
),
header: Row(
children: [
Text(
'Popular',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
SizedBox(
width: 5,
),
Text(
'Movies',
style: TextStyle(
fontSize: 22,
),
),
],
),
collapsed: SizedBox(
height: 2,
),
expanded: SizedBox(
width: double.infinity,
height: 200,
child: ListView.separated(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => movieItem(),
separatorBuilder: (context, index) => SizedBox(
width: 10,
),
itemCount: 10,
),
),
),
SizedBox(
height: 10,
),
ExpandablePanel(
theme: ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
),
header: Row(
children: [
Text(
'Coming',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
SizedBox(
width: 5,
),
Text(
'Soon',
style: TextStyle(
fontSize: 22,
),
),
],
),
collapsed: SizedBox(
height: 2,
),
expanded: SizedBox(
width: double.infinity,
height: 200,
child: ListView.separated(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => movieItem(),
separatorBuilder: (context, index) => SizedBox(
width: 10,
),
itemCount: 10,
),
),
),
SizedBox(
height: 10,
),
ExpandablePanel(
theme: ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
),
header: Row(
children: [
Text(
'Now',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
SizedBox(
width: 5,
),
Text(
'Playing',
style: TextStyle(
fontSize: 22,
),
),
],
),
collapsed: SizedBox(
height: 2,
),
expanded: SizedBox(
width: double.infinity,
height: 200,
child: ListView.separated(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => movieItem(),
separatorBuilder: (context, index) => SizedBox(
width: 10,
),
itemCount: 10,
),
),
),
],
),
),
),
);
},
),
);
}
替换
MoviesCubit()..getTopRatedData();
和
return MoviesCubit()..getTopRatedData();
或替换
create: (BuildContext context) {
MoviesCubit()..getTopRatedData();
}
和
create: (_) -> MoviesCubit()..getTopRatedData()
错误一:
error: 'Null' doesn't conform to the bound 'BlocBase<Object?>' of the type parameter 'T'. (type_argument_not_matching_bounds
错误 2:
error: Couldn't infer type parameter 'T'.
Tried to infer 'Null' for 'T' which doesn't work:
Type parameter 'T' is declared to extend 'BlocBase<Object?>' producing 'BlocBase<Object?>'.
The type 'Null' was inferred from:
Parameter 'create' declared as 'T Function(BuildContext)'
but argument is 'Null Function(BuildContext)'.
考虑将显式类型参数传递给泛型。
(could_not_infer 在 [g_movies] lib\layout\movies_layout.dart:12)
class MoviesLayout extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( create: (BuildContext context) { MoviesCubit()..getTopRatedData(); }, child: BlocConsumer<MoviesCubit, MoviesStates>( listener: (context, state) {}, builder: (context, state) { return Scaffold( appBar: AppBar( title: Text( 'GMovies', style: GoogleFonts.oswald( fontWeight: FontWeight.bold, fontSize: 24, color: defaultColor, ), ), actions: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child: CircleAvatar( radius: 20, child: IconButton( icon: Icon(Icons.person), onPressed: () {}, ), ), ), ], ), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ ExpandablePanel( theme: ExpandableThemeData( headerAlignment: ExpandablePanelHeaderAlignment.center, ), header: Row( children: [ Text( 'Top Rated', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 22, ), ), SizedBox( width: 5, ), Text( 'Movies', style: TextStyle( fontSize: 22, ), ), ], ), collapsed: SizedBox( height: 2, ), expanded: SizedBox( width: double.infinity, height: 200, child: ListView.separated( shrinkWrap: true, scrollDirection: Axis.horizontal, itemBuilder: (context, index) => movieItem(), separatorBuilder: (context, index) => SizedBox( width: 10, ), itemCount: 10, ), ), ), SizedBox( height: 10, ), ExpandablePanel( theme: ExpandableThemeData( headerAlignment: ExpandablePanelHeaderAlignment.center, ), header: Row( children: [ Text( 'Popular', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 22, ), ), SizedBox( width: 5, ), Text( 'Movies', style: TextStyle( fontSize: 22, ), ), ], ), collapsed: SizedBox( height: 2, ), expanded: SizedBox( width: double.infinity, height: 200, child: ListView.separated( shrinkWrap: true, scrollDirection: Axis.horizontal, itemBuilder: (context, index) => movieItem(), separatorBuilder: (context, index) => SizedBox( width: 10, ), itemCount: 10, ), ), ), SizedBox( height: 10, ), ExpandablePanel( theme: ExpandableThemeData( headerAlignment: ExpandablePanelHeaderAlignment.center, ), header: Row( children: [ Text( 'Coming', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 22, ), ), SizedBox( width: 5, ), Text( 'Soon', style: TextStyle( fontSize: 22, ), ), ], ), collapsed: SizedBox( height: 2, ), expanded: SizedBox( width: double.infinity, height: 200, child: ListView.separated( shrinkWrap: true, scrollDirection: Axis.horizontal, itemBuilder: (context, index) => movieItem(), separatorBuilder: (context, index) => SizedBox( width: 10, ), itemCount: 10, ), ), ), SizedBox( height: 10, ), ExpandablePanel( theme: ExpandableThemeData( headerAlignment: ExpandablePanelHeaderAlignment.center, ), header: Row( children: [ Text( 'Now', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 22, ), ), SizedBox( width: 5, ), Text( 'Playing', style: TextStyle( fontSize: 22, ), ), ], ), collapsed: SizedBox( height: 2, ), expanded: SizedBox( width: double.infinity, height: 200, child: ListView.separated( shrinkWrap: true, scrollDirection: Axis.horizontal, itemBuilder: (context, index) => movieItem(), separatorBuilder: (context, index) => SizedBox( width: 10, ), itemCount: 10, ), ), ), ], ), ), ), ); }, ), ); }
替换
MoviesCubit()..getTopRatedData();
和
return MoviesCubit()..getTopRatedData();
或替换
create: (BuildContext context) {
MoviesCubit()..getTopRatedData();
}
和
create: (_) -> MoviesCubit()..getTopRatedData()