命名参数未定义:

时间:2021-06-16 21:01:40

标签: flutter dart

自从空安全更新发布以来,我一直遇到问题。我搜索了很多,但找不到足够的资源。

class AllCoins {
  String symbol;
  String iconUrl;
  double marketCap;
  double price;

  AllCoins(this.symbol, this.iconUrl, this.marketCap, this.price);
  factory AllCoins.fromJson(dynamic json) {
    return AllCoins(
      symbol: json["symbol"],
      iconUrl: json["iconUrl"],
      marketCap: json["marketCap"],
      price: json["price"],
    );
  }
}

error: 4 positional argument(s) expected, but 0 found. 
error: The named parameter 'symbol' isn't defined.
error: The named parameter 'iconUrl' isn't defined. 
error: The named parameter 'marketCap' isn't defined. 
error: The named parameter 'price' isn't defined. 

实际上我只是想获取具有空安全性的数据,但没有有用的视频。我仍在努力学习 flutter 和 dart,但随着更新的到来变得越来越难。

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为您在需要位置参数的地方使用了命名参数

要使用命名参数,通过在参数周围添加 import { View } from "react-native" import Carousel from 'react-native-snap-carousel' import CardItem, { SLIDER_WIDTH, ITEM_WIDTH } from './CardItem' import { cardData } from '../data' const CarouselCards = () => { const isCarousel = React.useRef(null) return ( <View> <Carousel layout="default" layoutCardOffset={9} ref={isCarousel} data={cardData} renderItem={CardItem} sliderWidth={SLIDER_WIDTH} itemWidth={ITEM_WIDTH} inactiveSlideShift={0} useScrollView={true} onSnapToItem={(index) => setIndex(index)} /> </View> ) } export default CarouselCards [1]: https://reactnative.dev/docs/modal { 来更新构造函数

}

要继续使用位置参数,请更新 AllCoins({required this.symbol,required this.iconUrl,required this.marketCap, required this.price});

fromJson