Я получаю ошибку _TypeError при использовании StreamBuilder во флаттере
Попытка изучить шаблон Flutter и Bloc путем создания проверки формы с использованием шаблона Flutter. Кажется, я не понимаю, что не так с моим кодом.
Это файл block.dart
class Bloc extends Object with Validator{
final _email=StreamController<String>();
Stream<String> get email=>_email.stream.transform(ValidatEmail);
Function(String) get changeEmail => _email.sink.add;
dispose(){
_email.close();
}
}
final bloc = Bloc();
Вот LoginScreen.dart
class LoginScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(20.0),
child: Column(
children: [
emailField(),
],
),
);
}
Widget emailField(){
return StreamBuilder(
stream: bloc.email,
builder: (context,snapshot){
return TextField(
onChanged: bloc.changeEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
errorText: snapshot.error,
hintText: 'you@example.com',
labelText: 'Email Address',
),
);
},
);
}
}
Вот Validator.dart
class Validator{
final ValidatEmail = StreamTransformer<String,String>.fromHandlers(
handleData: (email,sink){
if(email.contains('@')){
sink.add(email);
}else{
sink.addError('Enter a valid email');
}
}
);
}
Я получаю следующую ошибку
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building LoginScreen(dirty):
type '_AsyncStreamController<dynamic>' is not a subtype of type 'StreamController<String>' of 'function result'
The relevant error-causing widget was:
LoginScreen file:///C:/Users/HP/Desktop/Olds/SIH/login_bloc/lib/src/app.dart:9:15
When the exception was thrown, this was the stack:
#0 Bloc._email (package:loginbloc/bloc/bloc.dart)
#1 Bloc.email (package:loginbloc/bloc/bloc.dart:8:29)
#2 LoginScreen.emailField (package:loginbloc/screen/LoginScreen.dart:22:20)
#3 LoginScreen.build (package:loginbloc/screen/LoginScreen.dart:12:11)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4585:28)