英文:
Flutter error: The argument type 'NumberTrivia' can't be assigned to the parameter type 'NumberTriviaModel'
问题
Here is the translated content you provided:
1. number_trivia.dart
import 'package:equatable/equatable.dart';
class NumberTrivia extends Equatable {
final String text;
final int number;
NumberTrivia({
required this.text,
required this.number,
}) : super();
}
2. number_trivia_model.dart
import '../../domain/entities/number_trivia.dart';
class NumberTriviaModel extends NumberTrivia {
NumberTriviaModel({
required String text,
required int number,
}) : super(text: text, number: number);
factory NumberTriviaModel.fromJson(Map<String, dynamic> json) {
return NumberTriviaModel(
text: json["text"],
number: (json["number"] as num).toInt(),
);
}
Map<String, dynamic> toJson() => {
"text": text,
"number": number,
};
}
3. number_trivia_repository_impl.dart // The error file
import 'package:dartz/dartz.dart';
import 'package:meta/meta.dart';
import '../../../../core/error/exception.dart';
import '../../../../core/error/failures.dart';
import '../../../../core/platform/network_info.dart';
import '../../domain/entities/number_trivia.dart';
import '../../domain/repositories/number_trivia_repository.dart';
import '../datasources/number_trivia_local_data_source.dart';
import '../datasources/number_trivia_remote_data_source.dart';
typedef Future<NumberTrivia> _ConcreteOrRandomChooser();
class NumberTriviaRepositoryImpl implements NumberTriviaRepository {
final NumberTriviaRemoteDataSource remoteDataSource;
final NumberTriviaLocalDataSource localDataSource;
final NetworkInfo networkInfo;
NumberTriviaRepositoryImpl(
@required this.remoteDataSource,
@required this.localDataSource,
@required this.networkInfo,
);
@override
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(
int number,
) async {
return await _getTrivia(() {
return remoteDataSource.getConreteNumberTrivia(number);
});
}
@override
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia() async {
return await _getTrivia(() {
return remoteDataSource.getRandomNumberTrivia();
});
}
Future<Either<Failure, NumberTrivia>> _getTrivia(
_ConcreteOrRandomChooser getConcreteOrRandom,
) async {
if (await networkInfo.isConnected) {
try {
final remoteTrivia = await getConcreteOrRandom();
localDataSource.cacheNumberTrivia(remoteTrivia); // The error
return Right(remoteTrivia);
} on ServerException {
return Left(ServerFailure());
}
} else {
try {
final localTrivia = await localDataSource.getLastNumberTrivia();
return Right(localTrivia);
} on CacheException {
return Left(CacheFailure());
}
}
}
}
The issue you mentioned about the remoteTrivia
variable being declared as final
but still showing an error on that word would require further investigation of your code and IDE settings to determine the exact cause of the error.
英文:
I following a YouTube Flutter app series Where I cant figure out the error while in the video I cant see any error, but in my IDE there's showing an error:
1. number_trivia.dart
import 'package:equatable/equatable.dart';
class NumberTrivia extends Equatable {
final String text;
final int number;
NumberTrivia({
required this.text,
required this.number,
}) : super();
}
2. number_trivia_model.dart
import '../../domain/entities/number_trivia.dart';
class NumberTriviaModel extends NumberTrivia {
NumberTriviaModel({
required String text,
required int number,
}) : super(text: text, number: number);
factory NumberTriviaModel.fromJson(Map<String, dynamic> json) {
return NumberTriviaModel(
text: json["text"],
number: (json["number"] as num).toInt(),
);
}
Map<String, dynamic> toJson() => {
"text": text,
"number": number,
};
}
3. number_trivia_repository_impl.dart // The error file
import 'package:dartz/dartz.dart';
import 'package:meta/meta.dart';
import '../../../../core/error/exception.dart';
import '../../../../core/error/failures.dart';
import '../../../../core/platform/network_info.dart';
import '../../domain/entities/number_trivia.dart';
import '../../domain/repositories/number_trivia_repository.dart';
import '../datasources/number_trivia_local_data_source.dart';
import '../datasources/number_trivia_remote_data_source.dart';
typedef Future<NumberTrivia> _ConcreteOrRandomChooser();
class NumberTriviaRepositoryImpl implements NumberTriviaRepository {
final NumberTriviaRemoteDataSource remoteDataSource;
final NumberTriviaLocalDataSource localDataSource;
final NetworkInfo networkInfo;
NumberTriviaRepositoryImpl(
@required this.remoteDataSource,
@required this.localDataSource,
@required this.networkInfo,
);
@override
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(
int number,
) async {
return await _getTrivia(() {
return remoteDataSource.getConreteNumberTrivia(number);
});
}
@override
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia() async {
return await _getTrivia(() {
return remoteDataSource.getRandomNumberTrivia();
});
}
Future<Either<Failure, NumberTrivia>> _getTrivia(
_ConcreteOrRandomChooser getConcreteOrRandom,
) async {
if (await networkInfo.isConnected) {
try {
final remoteTrivia = await getConcreteOrRandom();
localDataSource.cacheNumberTrivia(remoteTrivia); // The error
return Right(remoteTrivia);
} on ServerException {
return Left(ServerFailure());
}
} else {
try {
final localTrivia = await localDataSource.getLastNumberTrivia();
return Right(localTrivia);
} on CacheException {
return Left(CacheFailure());
}
}
}
}
The remoteTrivia
variable was declared before as final
but still, it's showing the error on that word.
答案1
得分: 1
localDataSource.cacheNumberTrivia(remoteTrivia as NumberTriviaModel);
英文:
localDataSource.cacheNumberTrivia(remoteTrivia);
to this :
localDataSource.cacheNumberTrivia(remoteTrivia as NumberTriviaModel);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论