首页 > 解决方案 > 错误“在 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);
              },
            );
          },
        );
      }

    }

你能帮我弄清楚如何解决这个问题吗?

标签: fluttersearchdelegatesgetterbloc

解决方案


推荐阅读