繁体   English   中英

Flutter bloc_pattern 返回 NoSuchMethodError

[英]Flutter bloc_pattern returning NoSuchMethodError

我正在构建我的第一个 Flutter 应用程序,而且很愚蠢,我从复杂的结构开始,比如 MVVM,我对它不太了解,但已经尝试过了。

这是我的文件夹结构:

lib
---models
------videos.dart
---repos
------videos.dart
---viewmodels
------videos.dart
main.dart (I know)

在我的main.dart文件中,我使用 StreamBuilder 来获取视频列表,代码如下:

class Home extends StatelessWidget {
  VideosBloc videosBloc = BlocProvider.getBloc<VideosBloc>();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: CustomAppBar(),
        ),
        Expanded(
          flex: 9,
          child: Container(
            color: Theme.of(context).primaryColorDark,
            child: StreamBuilder(
              stream: videosBloc.videosStream,
              builder: (context, snapshot) {
                List<Video> videos = snapshot != null ? snapshot.data : [];
                return ListView(
                  children: videos.map((video) {
                    return VideoContainer(video);
                  }).toList(),
                );
              },
            ),
          ),
        )
      ],
    );
  }
}

有这个 bloc 文件:

import 'dart:async';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:peeke_vines/models/videos.dart';
import 'package:peeke_vines/repositories/videos.dart';
import 'package:rxdart/rxdart.dart';

class VideosBloc extends BlocBase {
  VideosBloc();

  //Stream that receives a number and changes the count;
  var _videosController =
      BehaviorSubject<List<Video>>.seeded(VideosRepo().getVideos());

  //output
  Stream<List<Video>> get videosStream => _videosController.stream;
  //input
  Sink<List<Video>> get videosSink => _videosController.sink;

  //dispose will be called automatically by closing its streams
  @override
  void dispose() {
    _videosController.close();
    super.dispose();
  }
}

这个集团从存储库中获取这些数据

import 'package:peeke_vines/models/videos.dart';

class VideosRepo {
  VideosRepo();

  List<Video> videos = VideoModel().fetchVideos();

  List<Video> getVideos() {
    return [];
  }
}

这个存储库目前只是从模型中获取数据,但稍后我将实现一个 Web 服务来从以下位置获取数据:

import 'package:meta/meta.dart';

class VideoModel {
  List<Video> fetchVideos() {
    return [
      Video(
          video: 'assets/videos/main.wmv',
          title: 'Peeke Vines Video Title',
          date: '2 Days ago',
          thumbnail: 'assets/thumbnails/1.jpg',
          duration: 13.5),
      Video(
          video: 'assets/videos/main.wmv',
          title: 'Peeke Vines Video Title',
          date: '2 Days ago',
          thumbnail: 'assets/thumbnails/2.jpg',
          duration: 13.5),
      Video(
          video: 'assets/videos/main.wmv',
          title: 'Peeke Vines Video Title',
          date: '2 Days ago',
          thumbnail: 'assets/thumbnails/1.jpg',
          duration: 13.5)
    ];
  }
}

class Video {
  String video, title, date, thumbnail;
  double duration;

  Video(
      {@required this.video,
      @required this.title,
      @required this.date,
      @required this.thumbnail,
      @required this.duration});
}

现在,每当我使用这一行在 main.dart 文件中导入 bloc 时:

VideosBloc videosBloc = BlocProvider.getBloc<VideosBloc>();

我收到此错误:

I/flutter ( 4338): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4338): The following NoSuchMethodError was thrown building MyApp(dirty):
I/flutter ( 4338): Class 'NoSuchMethodError' has no instance getter 'message'.
I/flutter ( 4338): Receiver: Instance of 'NoSuchMethodError'
I/flutter ( 4338): Tried calling: message
I/flutter ( 4338):
I/flutter ( 4338): When the exception was thrown, this was the stack:
I/flutter ( 4338): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter ( 4338): #1      BlocProvider.getBloc (package:bloc_pattern/src/bloc_provider.dart:47:12)
I/flutter ( 4338): #2      new Home (package:peeke_vines/main.dart:35:40)
I/flutter ( 4338): #3      MyApp.build (package:peeke_vines/main.dart:21:20)
I/flutter ( 4338): #4      StatelessElement.build (package:flutter/src/widgets/framework.dart:3974:28)
I/flutter ( 4338): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
I/flutter ( 4338): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
I/flutter ( 4338): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
I/flutter ( 4338): #8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
I/flutter ( 4338): #9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
I/flutter ( 4338): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
I/flutter ( 4338): #11     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:939:16)
I/flutter ( 4338): #12     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:910:5)
I/flutter ( 4338): #13     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:856:17)
I/flutter ( 4338): #14     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2320:19)
I/flutter ( 4338): #15     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:855:13)

你能告诉我为什么会出现这个错误吗?

问题是您试图在构建方法之外获取 Bloc。 尝试在构建方法开始时获取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM