flutter - 错误“在 null 上调用了 getter 'signUpBloc'。接收方:null 在颤振上使用 searchdelegate 时尝试调用:signUpBloc'
问题描述
我试图通过 searchdelegate 获取一些信息并将其放在一个包含所有用户信息的块上,但我收到错误“getter 'signUpBloc' 被调用为 null。接收器:null。尝试调用:signUpBloc” ,就像该集团从未初始化,但我知道如何不调用 null 因为信息是从搜索委托中选择的。
这是提供者的代码
import 'package:flutter/material.dart';
import 'package:auth_test1/src/blocs/signup_bloc.dart';
export 'package:auth_test1/src/blocs/signup_bloc.dart';
class Provider extends InheritedWidget {
final signUpBloc = SignUpBloc();
Provider({Key key, Widget child})
: super(key:key, child: child);
@override
bool updateShouldNotify( InheritedWidget oldWidget )=> true;
static SignUpBloc of ( BuildContext context ){
return ( context.dependOnInheritedWidgetOfExactType(aspect: Provider) as Provider ).signUpBloc;
}
}
似乎第一个错误在这个堆栈上
return ( context.dependOnInheritedWidgetOfExactType(aspect: Provider) as Provider ).signUpBloc;
第二个错误在搜索委托代码中的这个堆栈上
@override
Widget buildResults(BuildContext context) {
final bloc = Provider.of(context);
这是 searchDelegate 的代码
import 'package:flutter/material.dart';
import 'package:auth_test1/src/providers/provider.dart';
class SchoolSearch extends SearchDelegate {
String seleccion= '';
final colegios = [
'Gimnasio La Montaña',
'Gimnasio del Norte',
'Gimnasio Tilatá',
'Gimnasio Campestre',
'Colegio Nueva Granada',
'Gimnasio Los Portales',
'Gimnasio Vermont',
'Gimnasio Moderno',
'Gimnasio Femenino',
'Gimnasio Marymount',
'Colegio los Nogales',
];
@override
List<Widget> buildActions(BuildContext context) {
// acciones de la barra de busqueda
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: (){
query = '';
},
)
];
}
@override
Widget buildLeading(BuildContext context) {
// icono a la izquierda de la barra de busqueda
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: (){
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
final bloc = Provider.of(context);
//Crea los resultados de la busqueda
return Center(
child: Card(
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(20.0)
),
elevation: 25.0,
child: Container(
height: 150.0,
width: 200.0,
decoration: BoxDecoration(
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('Haz seleccionado el $seleccion', style: TextStyle(fontSize: 15.0 ,color: Colors.black54), textAlign: TextAlign.center,),
SizedBox(height: 10.0,),
RaisedButton(
color: Color.fromRGBO(0, 148, 152, 1),
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(15.0)
),
elevation: 0.0,
child: Container(
color: Colors.transparent,
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5),
child: Text('Confirmar',style: TextStyle(fontSize: 18.0 ,color: Colors.white,)),
),
onPressed: () {
if(seleccion.isNotEmpty){
bloc.changeColegio(seleccion);
}
},
),
],
)
),
),
);
}
@override
Widget buildSuggestions(BuildContext context) {
// Sugerencias a medida que escribe
final listaSugerida = (query.isEmpty)
? colegios
: colegios.where(
(c)=>c.toLowerCase().startsWith(query.toLowerCase())
).toList();
return ListView.builder(
itemCount: listaSugerida.length,
itemBuilder: (context, i){
return ListTile(
leading: Icon(Icons.school),
title: Text(listaSugerida[i]),
onTap: (){
seleccion=listaSugerida[i];
showResults(context);
},
);
},
);
}
}
你能帮我弄清楚如何解决这个问题吗?
解决方案
推荐阅读
- node.js - Nginx 反向代理路径名冲突
- javascript - 使用 jquery POST 方法向服务器发送登录请求
- java - 使用 SnpSift,只有 0.52% 的 VCF 被 dbsnp 数据库注释
- python - pytorch+tensorboard 错误“AttributeError:‘Tensor’对象没有属性‘items’”
- javascript - VS 代码不建议声明的函数
- angular - 如何在 Angular 8+ 中迭代 RXJS 可观察对象数组 [{},{}...] 并将每个对象的属性一次推送到模板一个对象
- nativescript - 如何在 nativescript-exoplayer 中捕获 PlayerError 并显示适当的警报
- javascript - 按钮在javascript中显示/隐藏div,切换不起作用
- python - 遍历 url 列表时无法建立连接
- python - Python,Oracle_cx,使用查询结果作为参数列表进入循环插入语句