我有这个错误;代币有问题吗?它说Uri无效令牌,但我事先登录了,为什么我不能读取它?头我添加令牌这样..但它不工作,如何解决它?我能做错什么吗?
所以map, ifwhere, where或其他东西不起作用。我是这个平台的新手,我可能问了一些很简单的问题,很抱歉。谢谢大家....
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_knowin_app/core/data/response.dart';
import 'package:flutter_knowin_app/core/util/constants.dart';
import 'package:flutter_knowin_app/features/promotions/domain/entities/check.dart';
import 'package:flutter_knowin_app/features/promotions/domain/entities/spin.dart';
import 'package:flutter_knowin_app/features/promotions/domain/repositories/spin_repository.dart';
import 'package:flutter_knowin_app/features/promotions/domain/usecases/check_regular.dart';
import 'package:flutter_knowin_app/features/promotions/domain/usecases/check_vip.dart';
import 'package:flutter_knowin_app/injection_container.dart';
part 'spin_event.dart';
part 'spin_state.dart';
enum SpinPageState {
regular,
vip,
}
class SpinBloc extends Bloc<SpinEvent, SpinState> {
final SpinRepository spinRepository = sl<SpinRepository>();
SpinPageState spinPageState;
bool stateVip;
bool spinState = false;
String winThing;
int winResult;
int gamePlayCountVip;
int gamePlayCountRegular;
checksRegular ChecksRegular;
checksVip ChecksVip;
SpinBloc({
@required checksRegular ChecksRegular,
@required checksVip ChecksVip,
})
: assert(checksRegular != null),
assert(checksVip != null),
ChecksRegular = ChecksRegular,
ChecksVip = ChecksVip,
super(SpinState.initial());
@override
Stream<SpinState> mapEventToState(SpinEvent event,) async* {
final SpinRepository spinRepository = sl<SpinRepository>();
SpinState.initial();
if (event is SpinLoadingEvent) {
Response updateResponse;
yield SpinLoadingState();
final checkRegularOrFailure = await ChecksRegular(
checksRegularParams());
final checkVipOrFailure = await ChecksVip(
checksVipParams());
checkRegularOrFailure.fold((failure) {
updateResponse = Response(
status: false,
message: UNEXPECTED_FAILURE_MESSAGE,
statusCode: "FAIL CHECK REGULAR",
);
}, (response) {
spinState = true;
spinPageState = SpinPageState.regular;
response.toList().where((e){
print("PEOPLE COUNT= ${ e.gamePlayed}");
});
});
checkVipOrFailure.fold((failure) {
updateResponse = Response(
status: false,
message: UNEXPECTED_FAILURE_MESSAGE,
statusCode: "FAIL CHECK VIP",
);
}, (response) {
spinState = true;
spinPageState = SpinPageState.vip;
});
yield SpinLoadingState();
}
if (event is SpinStartEvent) {
}
}
}
RemoteCode;
class PromotionRemoteDataSourceImpl implements PromotionRemoteDataSource {
String token;
final http.Client client;
PromotionRemoteDataSourceImpl({this.client});
Map<String, String> get defaultHeaders =>
{
'Accept': 'application/json',
'Authorization': 'Bearer $token',
"Content-Type": "application/json"
};
Map<String, String> get defaultQueryParams => {
"lang": Platform.localeName.split('_')[0],
};
@override
Future<List<Check>> checksRegular() async {
Uri uri = Uri.https(
API_URL,
'/promotions/check/regular',
defaultQueryParams,
);
print("URİ: "+ uri.toString());
final response = await client.get(uri, headers: defaultHeaders);
final body= json.decode(response.body);
我这样解决了这个错误;
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
sl<PromotionRemoteDataSource>().token = userState.token;//!!!
});
}
我认为问题就在这里
response.toList().where((e){
print("PEOPLE COUNT= ${ e.gamePlayed}");
});
原因如错误信息所述。响应变量是null,你试图将null转换为列表而Dart不喜欢这样。实现一些方法来处理响应为null的场景,例如在那里有一个if语句,这应该有望解决您的问题