【问题标题】:error: The argument type 'Object?' can't be assigned to the parameter type 'String'错误:参数类型“对象?”不能分配给参数类型“字符串”
【发布时间】:2021-09-03 07:29:44
【问题描述】:

问题很清楚,我只是不知道如何解决它......

这是整个页面:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_practice_part2/controller/user_notifier.dart';
import 'package:provider_practice_part2/models/user.dart';
import 'package:provider_practice_part2/screens/user_list_screen.dart';
import 'package:provider_practice_part2/widgets/cheetah_button.dart';
import 'package:provider_practice_part2/widgets/cheetah_input.dart';
import 'package:provider_practice_part2/widgets/user_list.dart';

class Home extends StatefulWidget {
  @override
  HomeState createState() => HomeState();
}

class HomeState extends State<Home> {
  String? _name;
  String? _city;

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    UserNotifier userNotifier = Provider.of<UserNotifier>(context);
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      appBar: AppBar(
        title: Consumer(
          builder: (context, title, child) {
            return Text(
              title,
              style: TextStyle(color: Colors.white),
            );
          },
        ),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(32),
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CheetahInput(
                labelText: 'Name',
                onSaved: (String? value) {
                  _name = value;
                },
              ),
              SizedBox(height: 16),
              CheetahInput(
                labelText: 'City',
                onSaved: (String? value) {
                  _city = value;
                },
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CheetahButton(
                    text: 'Add',
                    onPressed: () {
                      if (!_formKey.currentState!.validate()) return;

                      _formKey.currentState!.save();
                      context
                          .read<UserNotifier>()
                          .addUser(User(_name!, _city!));
                    },
                  ),
                  SizedBox(width: 8),
                  CheetahButton(
                    text: 'List',
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => UserListScreen(),
                        ),
                      );
                    },
                  ),
                ],
              ),
              SizedBox(height: 20),
              UserList(),
            ],
          ),
        ),
      ),
    );
  }
}

问题就在这里:

Consumer(
          builder: (context, title, child) {
            return Text(
              title,
              style: TextStyle(color: Colors.white),
            );
          },
        ),

正如您从标题中看到的那样,当我尝试将数据从消费者传递到文本小部件时出现错误,我已经尝试了 Null_aware 运算符和可空运算符,但它不起作用..... 毕竟我已经通过删除整个消费者并用这段代码替换它来解决问题:

Text(
          context.watch<String>(),
          style: TextStyle(color: Colors.white),
        ),

我只是想知道这个问题是否有任何解决方案,这对我和其他未来的程序员看到这个问题会很有用。

注意:我使用提供者 5 注意:这个调用是一些人工数据的未来,只是为了测试 futureprovider 的工作方式。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你只需要使用

    Consumer<String>(
    ...
    )
    

    【讨论】:

      【解决方案2】:

      当你使用Consumer 时,你需要传递一个泛型类型参数来让类知道你想使用什么,就像你对context.watch&lt;String&gt;() 所做的一样。由于 Dart 中泛型的工作方式,如果您不指定类型,Dart 将尝试使用类型推断来自动选择类型。但由于无法从 Consumer 的主体中推断出任何内容,Dart 将默认为最基本的类型,恰好是 Object?

      这很重要,不仅让代码知道title 将是什么类型,还因为Consumer 需要知道在所有父提供者中寻找什么类型,它必须筛选才能知道哪一个最终听。

      Consumer<String>(
        builder: (context, title, child) {
          return Text(
            title,
            style: TextStyle(color: Colors.white),
          );
        },
      ),
      

      【讨论】:

        猜你喜欢
        • 2021-10-10
        • 2021-07-17
        • 2021-12-14
        • 1970-01-01
        • 2021-06-19
        • 2021-12-20
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多