gpt4 book ai didi

dart - 在 Flutter 中使用 Stream/Sink

转载 作者:IT王子 更新时间:2023-10-29 06:38:42 27 4
gpt4 key购买 nike

我正在尝试使用 Streams 替换 increment flutter 应用程序代码从 Dart API 不使用 scoped_modelrxdart .

所以我读了this并观看了this ,但无法让它为我工作,我的代码是:

StreamProvider.dart:

import 'package:flutter/widgets.dart';
import 'businessLogic.dart';
import 'dart:async';

class Something {

final _additionalContrllerr = StreamController<int>();
Sink<int> get addition => _additionalContrllerr.sink;

Stream<int> get itemCount => _additionalContrllerr.stream;
}

class StreemProvider extends InheritedWidget {
final Something myBloc; // Business Logic Component

StreemProvider({
Key key,
@required this.myBloc,
Widget child,
}) : super(key: key, child: child);

@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;

static Something of(BuildContext context) =>
(context.inheritFromWidgetOfExactType(StreemProvider) as StreemProvider)
.myBloc;
}

main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_app/StreemProvider.dart';

void main() => runApp(MyApp(
textInput: Text("Provided By the Main"),
));

class MyApp extends StatefulWidget {
final Widget textInput;
MyApp({this.textInput});

@override
State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
bool checkBoxValue = false;

int _counter = 0;
@override
Widget build(BuildContext ctxt) {
var x = Something(); //// Not sure if have to use this!
return StreemProvider(
myBloc: x, //// Not sure about this!!
child: MaterialApp(
home: SafeArea(
child: Scaffold(
body: new Center(
child: new Column(
children: <Widget>[
widget.textInput,
Text("clickec $_counter times"),
Text("clickec ${x.itemCount.listen((int i) => i)} times"),
/// How to get the value of i??!
Checkbox(
value: checkBoxValue,
onChanged: (bool newValue){
setState(() {
checkBoxValue = newValue;
});
}
)
],
)),
floatingActionButton: Incrementer(_increment),
// floatingActionButton: Incrementer(x),
),
),
),
);
}

_increment() {
setState(() {
_counter += 1;
});
}
}

class Incrementer extends StatefulWidget {

final Function increment;

Incrementer(this.increment);

@override
State<StatefulWidget> createState() {
return IncrementerState();
}
}
class IncrementerState extends State<Incrementer>{
@override
Widget build(BuildContext ctxt) {
final myBloc = StreemProvider.of(context);
return new FloatingActionButton(
//onPressed: widget.increment,
// How ot get the latest value!!
onPressed: () async {
var y = await myBloc.itemCount.last;
if (y.isNaN) y = 0;
myBloc.addition.add(y+1);
},
child: new Icon(Icons.add),
);
}
}

最佳答案

不知道rx_dart有什么限制,只能通过你使用来尝试回答。哈哈

你的 bloc 没有定义在你的输入流中监听什么,这就是我如何让它工作的

counter_bloc.dart

import 'package:rxdart/rxdart.dart';
import 'dart:async';

class CounterBloc {
int _count = 0;

ReplaySubject<int> _increment = ReplaySubject<int>();
Sink<int> get increment => _increment;

BehaviorSubject<int> _countStream = BehaviorSubject<int>(seedValue: 0);
Stream<int> get count => _countStream.stream;

CounterBloc() {
_increment.listen((increment) {
_count += increment;
_countStream.add(_count);
});
}
}

在构造函数中,为该流设置了 listen 方法。对于发送的每个增量,它都会增加计数器并将当前计数发送到另一个流。

main.dart 中,删除了 _counter 属性,因为它现在由 BLOC 处理。并显示我使用了流生成器。

还添加了第二个 fab,以 +2 增量来测试逻辑。

希望这可以帮助您为 bloc 类建模。 :)

一个很好的 Bloc 引用:https://www.youtube.com/watch?v=PLHln7wHgPE

main.dart

import 'counter_bloc.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

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

class _MyHomePageState extends State<MyHomePage> {
CounterBloc bloc = CounterBloc();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
StreamBuilder<int>(
stream: bloc.count,
initialData: 0,
builder: (BuildContext c, AsyncSnapshot<int> data) {
return Text(
'${data.data}',
style: Theme.of(context).textTheme.display1,
);
},
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: () {
bloc.increment.add(2);
},
tooltip: 'Increment 2',
child: Text("+2"),
),
FloatingActionButton(
onPressed: () {
bloc.increment.add(1);
},
tooltip: 'Increment 1',
child: Text("+1"),
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

关于dart - 在 Flutter 中使用 Stream/Sink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50995237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com