Вот ошибка. Я пытаюсь отправить текст на другую страницу после получения его из текстового поля. Для этого я использую блок.
I / трепетание (28636): ══╡ ИСКЛЮЧЕНИЕ ПРОВЕДЕНО БИБЛИОТЕКОЙ ВИДЖЕТОВ ╞═════════════════════════════════════════════════════════════════ I / flutter (28636): Следующая ошибка NoSuchMethodError была брошена в здание MediaQuery (MediaQueryData)(размер: Размер (360,0, I / флаттер (28636): 672,0), DevicePixelRatio: 2.0, textScaleFactor: 1.0, platformBrightness: Brightness.light, отступ: I / флаттер (28636): EdgeInsets.zero, viewPadding: EdgeInsets.zero,viewInsets: EdgeInsets.zero, PhysicalDepth: I / flutter (28636): 1.7976931348623157e + 308, AlwaysUse24HourFormat: false, availableNavigation: false, I / flutter (28636): disableAnimations: false, invertColors: false, boldText:):/ flutter (28636): получатель «состояние» был вызван на ноль. I / flutter (28636): получатель: null I / flutter (28636): пробный вызов: состояние I / flutter (28636): I / flutter (28636): созданный пользователем предок виджета, вызывающего ошибки, был: I / flutter(28636): Scaffold 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();