Flutter error: The argument type 'NumberTrivia' can't be assigned to the parameter type 'NumberTriviaModel'

huangapple go评论69阅读模式
英文:

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 &#39;package:equatable/equatable.dart&#39;;

class NumberTrivia extends Equatable {
  final String text;
  final int number;

  NumberTrivia({
    required this.text,
    required this.number,
  }) : super();
}

2. number_trivia_model.dart

import &#39;../../domain/entities/number_trivia.dart&#39;;

class NumberTriviaModel extends NumberTrivia {
  NumberTriviaModel({
    required String text,
    required int number,
  }) : super(text: text, number: number);

  factory NumberTriviaModel.fromJson(Map&lt;String, dynamic&gt; json) {
    return NumberTriviaModel(
      text: json[&quot;text&quot;],
      number: (json[&quot;number&quot;] as num).toInt(),
    );
  }

  Map&lt;String, dynamic&gt; toJson() =&gt; {
        &quot;text&quot;: text,
        &quot;number&quot;: number,
      };
}

3. number_trivia_repository_impl.dart // The error file

import &#39;package:dartz/dartz.dart&#39;;
import &#39;package:meta/meta.dart&#39;;

import &#39;../../../../core/error/exception.dart&#39;;
import &#39;../../../../core/error/failures.dart&#39;;
import &#39;../../../../core/platform/network_info.dart&#39;;
import &#39;../../domain/entities/number_trivia.dart&#39;;
import &#39;../../domain/repositories/number_trivia_repository.dart&#39;;
import &#39;../datasources/number_trivia_local_data_source.dart&#39;;
import &#39;../datasources/number_trivia_remote_data_source.dart&#39;;

typedef Future&lt;NumberTrivia&gt; _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&lt;Either&lt;Failure, NumberTrivia&gt;&gt; getConcreteNumberTrivia(
    int number,
  ) async {
    return await _getTrivia(() {
      return remoteDataSource.getConreteNumberTrivia(number);
    });
  }

  @override
  Future&lt;Either&lt;Failure, NumberTrivia&gt;&gt; getRandomNumberTrivia() async {
    return await _getTrivia(() {
      return remoteDataSource.getRandomNumberTrivia();
    });
  }

  Future&lt;Either&lt;Failure, NumberTrivia&gt;&gt; _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);

huangapple
  • 本文由 发表于 2023年5月7日 23:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定