【问题标题】:type 'Null' is not a subtype of type 'String' in type cast类型“Null”不是类型转换中“String”类型的子类型
【发布时间】:2023-03-03 15:20:02
【问题描述】:

我正在制作一个基本的购物应用程序。我无法弄清楚我犯的错误,因为我的 IDE 没有显示任何错误

这是错误

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


class EditProductScreen extends StatefulWidget {
  static const routeName = '/edit-product';
  @override
  _EditProductScreenState createState() => _EditProductScreenState();
}

class _EditProductScreenState extends State<EditProductScreen> {
  final _priceFocusNode = FocusNode();
  final _descriptionFocusNode = FocusNode();
  final _imageUrlController = TextEditingController();
  final _imageUrlFocusNode = FocusNode();
  final _form = GlobalKey<FormState>();
  

var _editedProduct = Product( id: '', title: '', price: 0, description: '', imageUrl: '', ); var _initvalues = { '标题': '', '描述': '', '价钱': '', 'imageUrl': '' };

  var _isInit = true;
  var _isLoading = false;

  @override
  void initState() {
    _imageUrlFocusNode.addListener(_updateImageUrl);

    super.initState();
  }

我想这是错误的地方,但我的 IDE 没有显示任何内容。

  @override
  void didChangeDependencies() { 
    if (_isInit) {
      final productId = ModalRoute.of(context)!.settings.arguments as  String;
      if (productId .isNotEmpty) {
        _editedProduct = Provider.of(context)<Products>(context, listen: false);
        _initvalues = {
          'title': _editedProduct.title,
          'description': _editedProduct.description,
          'price': _editedProduct.price.toString(),
          // 'imageurl': _editedProduct.imageUrl,
          'imageUrl': ''
        };
        _imageUrlController.text = _editedProduct.imageUrl;
      }
    }
    _isInit = false;
    super.didChangeDependencies();
  }

 

 

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试对您最后的 sn-p 代码进行一些更改。

    final productId = ModalRoute.of(context)!.settings.arguments.toString ?? "";
    

    【讨论】:

    • 最好处理空情况而不是强制提升。他可以将ModalRoute.of(context) 分配给一个局部变量并检查它是否是null,这样它就会被静态流分析提升。
    • @Aashar Wahla ,您更具体地说明要在引号之间添加什么
    • toString 调用后是不是少了括号?
    【解决方案2】:

    看来argumentsnull

    final args = ModalRoute.of(context)!.settings.arguments;
    if(args != null) {
      final productId = argument.toString();  // Hoping only string will be there.
      /// Rest of the code
    }
    

    【讨论】:

    • 很抱歉,这似乎不是我的问题的解决方案。有没有其他办法
    • 你是如何推动/调用这个屏幕的?有了给定的信息,现在很难说出确切的问题出在哪里。
    【解决方案3】:
    final productId = modulroute != null ? modulroute.settings.arguments : '';
    

    那么你在何时何地使用productId你使用

    productId.tostring();
    

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题,但过了一段时间我发现了使用这个的解决方案:

      final String? productId = ModalRoute.of(context)!.settings.arguments as String?;
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2021-11-07
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多