为什么在从文本字段获取文本后尝试将文本发送到另一个页面时出现错误,我为此使用了bloc

时间:2019-10-19 08:11:06

标签: flutter bloc

这里是错误。我尝试从文本字段获取文本后将文本发送到另一页。为此,我正在使用bloc。

  

I / flutter(28636):W小组件库引起的异常提示   ╞═════════════════════════════════════════════════ ══════════我/扑   (28636):引发了以下NoSuchMethodError构建   MediaQuery(MediaQueryData(size:Size(360.0,I / flutter(28636):672.0),   devicePixelRatio:2.0,textScaleFactor:1.0,platform亮度:   Brightness.light,padding:I / flutter(28636):EdgeInsets.zero,   viewPadding:EdgeInsets.zero,viewInsets:EdgeInsets.zero,   physicalDepth:I / flutter(28636):1.7976931348623157e + 308,   alwaysUse24HourFormat:false,可访问Navigation:false,I / flutter   (28636):disableAnimations:false,invertColors:false,boldText:   false)):I / flutter(28636):在空调用getter'state'。   I / flutter(28636):接收方:空I / flutter(28636):尝试调用:   状态I / flutter(28636):I / flutter(28636):用户创建的祖先   导致错误的小部件是:I / flutter(28636):脚手架   lib \ main.dart:166 I / flutter(28636):I / flutter(28636):当   引发异常,这是堆栈:I / flutter(28636):#0
  Object.noSuchMethod(dart:core-patch / object_patch.dart:51:5)   I / flutter(28636):#1 _BlocBuilderBaseState.didUpdateWidget

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:rxdart/rxdart.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // return BlocProvider(
    //   builder: (BuildContext context) => TextBloc(),
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Bloc Text',
      home: MyHomePage(),
      // ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage();

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String initialData = '';

  @override
  Widget build(BuildContext context) {
    TextEditingController _controller = new TextEditingController();
    //  final TextBloc bloc = BlocProvider.of<TextBloc>(context);

    return Scaffold(
      appBar:
          AppBar(backgroundColor: Colors.green[200], title: Text('Store Text')),
      body: Center(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.all(8.0),
              width: 300,
              color: Colors.amber,
              padding: const EdgeInsets.all(18.0),
              child: StreamBuilder(
                  initialData: initialData,
                  stream: bloc.textStream,
                  builder: (context, snapshot) {
                    return Text(
                      '${snapshot.hasData ? snapshot.data : ''}',
                      style: TextStyle(fontSize: 20),
                    );
                  }),
            ),
            Padding(
              padding: const EdgeInsets.all(18.0),
              child: StreamBuilder(
                  initialData: initialData,
                  stream: bloc.textStream,
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting)
                      _controller.text = snapshot.data;
                    return TextField(
                      controller: _controller,
                      onChanged: bloc.textChangedStream,
                      // decoration: InputDecoration(labelText: 'Enter name' ),
                      decoration: InputDecoration(
                        fillColor: Colors.grey[100],
                        filled: true,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.grey,
                          ),
                          borderRadius: BorderRadius.circular(6.0),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide:
                              BorderSide(color: Colors.blueAccent, width: 0.0),
                          borderRadius: BorderRadius.circular(6.0),
                        ),
                      ),
                    );
                  }),
            ),
            Container(
              height: 50,
              width: 200,
              child: StreamBuilder(
                  initialData: initialData,
                  stream: bloc.textStream,
                  builder: (context, snapshot) {
                    return RaisedButton(
                      child: Text('Open Next Page'),
                      onPressed: () {
                        Navigator.of(context).push(
                            MaterialPageRoute<SecondPage>(builder: (context) {
                          return BlocProvider.value(
                            //value: bloc,
                            child: SecondPage(),
                          );
                        }));
                      },
                    );
                  }),
            )
          ],
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String initialData = '';
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: BlocBuilder(
        builder: (context, text) {
          return Center(
            child: StreamBuilder(
                initialData: initialData,
                stream: bloc.textStream,
                builder: (context, snapshot) {
                  return Text('${snapshot.hasData ? snapshot.data : ''}');
                }),
          );
        },
      ),
    );
  }
}

class TextBloc {
  final StreamController textStreamController = BehaviorSubject<String>();
  Stream<String> get textStream => textStreamController.stream;
  Function(String) get textChangedStream => textStreamController.sink.add;
}

final TextBloc bloc = new TextBloc();

0 个答案:

没有答案