Flutter:参数类型“List< dynamic>”无法分配给参数类型“List< Service>

fhg3lkii  于 2023-05-08  发布在  Flutter
关注(0)|答案(1)|浏览(142)

我试图复制一个关于如何将webapi与flutter集成的例子,我遇到了这个错误。当尝试在services_bloc.dart中编码flutter块时

import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:testproject_flutter/Service/bloc/bloc.dart';
import '../repository/service_repository.dart';

class ServiceBloc extends Bloc<ServiceEvent, ServiceState> {
  final ServiceRepository serviceRepository;

  ServiceBloc({required this.serviceRepository})
      : assert(serviceRepository != null),
        super(ServiceLoading());

  Stream<ServiceState> mapEventToState(ServiceEvent event) async* {
    if (event is ServiceLoad) {
      print("Service load method");
      yield ServiceLoading();

      try {
        final services = await serviceRepository.getServices();
        print("This is the service $services");
        yield ServicesLoadSuccess(services);
      } catch (_) {
        yield ServiceOperationFailure();
      }
    }

错误来自yield ServicesLoadSuccess(services)。下面是服务引用的代码,我尝试调用. net6中的API来flutter进行app开发

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


@immutable
class Service extends Equatable{
  final  int  id;
  final String ServiceName;
  final String Description;
  final String Category;
  final String imageUrl;
  final int InitialPrice;
  final int IntermediatePrice;
  final int AdvancedPrice;


  const Service({
    required this.id,
    required this.ServiceName,
    required this.imageUrl ,
    required this.Description ,
    required this.Category,
    required this.InitialPrice ,
    required this.IntermediatePrice ,
    required this.AdvancedPrice,

  });

  @override
  List<Object> get props =>
   [id, ServiceName, imageUrl, Description, Category,InitialPrice,IntermediatePrice,AdvancedPrice];

  factory Service.fromJson(Map<String, dynamic> json) {
    return Service(
      id: json['serviceId'],
      ServiceName: json['serviceName'],
      imageUrl: json['imageUrl'],
      Description: json['description'],
      Category: json['category'],
      InitialPrice: json['initialPrice'],
      IntermediatePrice: json['intermediatePrice'],
      AdvancedPrice: json['advancedPrice'],
    );
  }

  @override
  String toString() => 'Service { id: $id, ServiceName: $ServiceName, Description: $Description }';
}

services是引用对象,下面是另一个块引用的service_repository.dart的代码

import 'package:meta/meta.dart';
import 'package:testproject_flutter/Service/model/service.dart';

import '../data_provider/service_data.dart';

class ServiceRepository {
  final ServiceDataProvider dataProvider;

  ServiceRepository({required this.dataProvider})
      : assert(dataProvider != null);

  Future<Service> createService(Service service) async {
    return await dataProvider.createService(service);
  }

  Future<List> getServices() async {
    print("This is the getService method");
    return await dataProvider.getServices();
  }

  Future<void> updateService(Service service) async {
    await dataProvider.updateService(service);
  }

  Future<void> deleteService(int  id) async {
    await dataProvider.deleteService(id);
  }
}

如何解决这个问题?

rwqw0loc

rwqw0loc1#

解决方案可以是以下之一或两者。在getServices方法中:

  • 将返回类型从Future<List>更改为Future<List<Service>>
  • 将返回值所在的行更改为:
final listOfServices = await dataProvider.getServices();

return List<Service>.from(
 listOfServices,
);

相关问题