简体   繁体   中英

Flutter bloc_pattern returning NoSuchMethodError

I am building my first Flutter app, and foolishly enough, I started off with complex structures such as the MVVM, I don't know very much about it but have tried.

Here is my folder structure:

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

In my main.dart file, I am using StreamBuilder to fetch the lisit of videos, here's the code:

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(),
                );
              },
            ),
          ),
        )
      ],
    );
  }
}

There there is this bloc file:

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();
  }
}

And this bloc gets this data from a repository

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

class VideosRepo {
  VideosRepo();

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

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

And this repository currently just gets the data from a model, but later I'll implement a web service to fetch the data from:

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});
}

Now, whenever I import the bloc in the main.dart file using this line:

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

I get this error:

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)

Can you tell me why this error is coming?

The issue is that you are trying to get the Bloc outside of the build method. Try to get it at the start of the build method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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