作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用库 hydred_bloc 保持状态,问题是该库的所有示例都非常基本,我想通过使用示例 api rest 来保持状态,但我仍然无法实现此示例的逻辑:
userbloc_bloc.dart
import 'dart:async';
import 'package:di/users/model/user.dart';
import 'package:di/users/repository/cloud_api_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
part 'userbloc_event.dart';
part 'userbloc_state.dart';
class UserblocBloc extends HydratedBloc<UserblocEvent, UserblocState> {
@override
UserblocState get initialState {
return super.initialState ?? UserblocInitial();
}
@override
UserblocState fromJson(Map<String, dynamic> json) {
try {
final usermodel = UserModel.fromJson(json);
return UserblocLoaded(usermodel);
} catch (_) {
return null;
}
}
@override
Map<String, dynamic> toJson(UserblocState state) {
if (state is UserblocLoaded) {
return state.userModel.toJson();
}else{
return null;
}
}
@override
Stream<UserblocState> mapEventToState(
UserblocEvent event,
) async* {
}
}
part of 'userbloc_bloc.dart';
abstract class UserblocState extends Equatable {
UserblocState([List props = const[]]);
}
class UserblocInitial extends UserblocState {
CloudApiRepository _cloudApiRepository;
@override
List<Object> get props => null;
Future<UserModel> getlistUser() => _cloudApiRepository.getlistUser();
}
class UserblocLoading extends UserblocState {
@override
Li
st<Object> get props => null;
}
class UserblocLoaded extends UserblocState {
final UserModel userModel;
UserblocLoaded(this.userModel);
@override
List<Object> get props => null;
}
import 'package:di/users/bloc/userbloc/userbloc_bloc.dart';
import 'package:di/users/model/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class HomeUser extends StatefulWidget {
HomeUser({Key key}) : super(key: key);
@override
_HomeUserState createState() => _HomeUserState();
}
class _HomeUserState extends State<HomeUser> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocProvider(create: (context) => UserblocBloc(),
child: BlocBuilder<UserblocBloc, UserblocState>(
builder: (BuildContext context, UserblocState state) {
if (state is UserblocInitial) {
return showanytext();
} else if (state is UserblocLoading) {
return buildLoading();
} else if (state is UserblocLoaded) {
return buildColumnWithData(state.userModel.data);
}
},
),
),
);
}
}
Widget buildColumnWithData(List<User> users){
final user = users;
return ListView.builder(
itemCount: users.length,
itemBuilder: (BuildContext context, int i){
return Container(
padding: EdgeInsets.all(15),
child: Text("${user[i].firstName}", style: TextStyle(color:Colors.white),),
decoration: BoxDecoration(
color: Colors.black
),
);
});
}
Widget buildLoading(){
return Center(
child: Container(
child: CircularProgressIndicator(),
),
);
}
Widget showanytext(){
return Center(
child: Container(
child: Text("Construyendo el widget"),
),
);
}
最佳答案
好问题 - 我有一个非常相似的问题。
简而言之 - 我的 Cubit 状态反射(reflect)了填写表单并将其发送到 API 的阶段: - 初始、 - 开始、 - 勾选、 - 已完成和 - 已发送。
由于无法确定 Hydrated Bloc 功能应该返回哪个状态,我实际上是将 state 属性作为枚举添加到 Cubit 状态,以反射(reflect) Cubit '最后一次出现' 的状态,并且在某些条件下返回相同来自存储库的状态。使用它,将获取最后填写的字段。
这听起来和看起来像是冗余,但我想不出更好的解决方案。我很乐意听到一些想法:)
这是 Cubit 的 State 类声明:
part of 'job_realization_cubit.dart';
enum Status {
initial,
started,
tick,
completed,
sent,
}
abstract class JobRealizationState extends Equatable {
const JobRealizationState({
this.status,
this.timeLeft,
this.jobRealization,
this.message,
});
final Status status;
final int timeLeft;
final JobRealization jobRealization;
final String message;
@override
List<Object> get props => [status, timeLeft, jobRealization, message];
}
class JobRealizationInitial extends JobRealizationState {
const JobRealizationInitial();
}
class JobRealizationStarted extends JobRealizationState {
const JobRealizationStarted({status, timeLeft, jobRealization, message})
: super(
status: status,
timeLeft: timeLeft,
jobRealization: jobRealization,
message: message,
);
}
//Timekeeping after start of Realization
class JobRealizationTick extends JobRealizationState {
const JobRealizationTick({status, timeLeft, jobRealization, message})
: super(
status: status,
timeLeft: timeLeft,
jobRealization: jobRealization,
message: message,
);
}
class JobRealizationDone extends JobRealizationState {
const JobRealizationDone({status, timeLeft, jobRealization, message})
: super(
status: status,
timeLeft: timeLeft,
jobRealization: jobRealization,
message: message,
);
}
class JobRealizationSent extends JobRealizationState {
const JobRealizationSent({status, timeLeft, jobRealization, message})
: super(
status: status,
timeLeft: timeLeft,
jobRealization: jobRealization,
message: message,
);
}
class JobRealizationSendError extends JobRealizationState {
const JobRealizationSendError({status, timeLeft, jobRealization, message})
: super(
status: status,
timeLeft: timeLeft,
jobRealization: jobRealization,
message: message,
);
}
关于flutter - 如何在 flutter 中使用 hydated_bloc 保持状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299542/
我正在尝试使用库 hydred_bloc 保持状态,问题是该库的所有示例都非常基本,我想通过使用示例 api rest 来保持状态,但我仍然无法实现此示例的逻辑: userbloc_bloc.dart
我是一名优秀的程序员,十分优秀!