delaram
delaram

Reputation: 819

error: The argument type 'Object?' can't be assigned to the parameter type 'String'

the problem is clear i just dont know hoe to solve it ...

here is the the entire page :

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(),
            ],
          ),
        ),
      ),
    );
  }
}

the problem is here :

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

as you can see from the title it has an error while I try to pass the data from the consumer to the text widget i have already tried the Null_aware operator and the nullable operator but it doesnt work ..... after all i have solved the problem by deleteting the entire consumer and replacing it with this code :

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

i just want to know if there is any solution to this problem it would be useful for me and other future programmers to see this question .

note : im using provider 5 note : this call is a future to some artificial data just to test how futureprovider work..

Upvotes: 1

Views: 962

Answers (2)

Abion47
Abion47

Reputation: 24616

When you use a Consumer, you need to pass a generic type parameter to let the class know what it is you are trying to consume, just like what you are doing with context.watch<String>(). Because of the way generics work in Dart, if you don't specify a type, Dart will try to use type inference to automatically choose a type. But since there isn't anything to infer from the body of Consumer, Dart will just default to the most basic type, which happens to be Object?.

This is important not just for letting the code know what type title will be, but also because the Consumer needs to know what type to look for in all the parent providers it has to sift through to know which one to ultimately listen to.

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

Upvotes: 0

towhid
towhid

Reputation: 3278

You just need to use

Consumer<String>(
...
)

Upvotes: 1

Related Questions