Home > Software engineering >  The argument type 'Object' can't be assigned to the parameter type 'String'
The argument type 'Object' can't be assigned to the parameter type 'String'

Time:10-26

This is my UI

enter image description here

In this, I used a date picker and in there shows "Birthday" word as an initial date, when the click selects the button then shows the dates, That works perfectly. But I wanna add shared Preferences to this When added that, after the selected word and close the app and reopened again then should show the date that is selected at the last.

EX:- When the user at the first time open the app then should show like this

enter image description here

and h or she select "2021/4/27" and after close app and reopening then should display

enter image description here

For that I tried and I added shared preferences but when I add declared variable as initial value then shows this error. enter image description here

My code

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class BirthdayScreen extends StatefulWidget {
  const BirthdayScreen({Key? key}) : super(key: key);

  @override
  State<BirthdayScreen> createState() => _BirthdayScreenState();
}

class _BirthdayScreenState extends State<BirthdayScreen> {
  // 1st dropdown button
  @override
  void initState() {
    super.initState();
    dropdownValueBirthday = birthday.first;
    checkValueBirthday();
  }

  //date picker
  DateTime? selectedDate;
  DateTime now = new DateTime.now();
  void showDatePicker() {
    DateTime mindate = DateTime(now.year - 2, now.month, now.day - 29);
    DateTime maxdate = DateTime(now.year - 1, now.month, now.day);
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext builder) {
          return Container(
            height: MediaQuery.of(context).copyWith().size.height * 0.25,
            color: Colors.white,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.date,
              initialDateTime: mindate,
              onDateTimeChanged: (value) {
                if (value != null && value != selectedDate) {
                  setState(() {
                    selectedDate = value;
                  });
                }
              },
              maximumDate: maxdate,
              minimumDate: mindate,
            ),
          );
        });
  }

  String? dropdownValueBirthday;
  List<String> birthday = [
    'Birthday',
  ];

  //IF "dropdownValueMembers" is empty pass "which" word as a initial value if al ready selected then pass the shared preference value
  checkValueBirthday() {
    _getDataBirthday();
  }

  _saveDataBirthday(String dropdownValueBirthdayShared) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("dataBirthday", dropdownValueBirthdayShared);
  }

  _getDataBirthday() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    dropdownValueBirthday =
        sharedPreferences.getString("dataBirthday") ?? birthday.first;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            Center(
              child: Padding(
                padding: const EdgeInsets.only(left: 15, top: 100),
                child: Row(
                  children: <Widget>[
                    const Icon(
                      Icons.brightness_1,
                      color: Colors.black,
                      size: 10,
                    ),
                    const Padding(
                      padding: EdgeInsets.only(left: 15.0),
                      child: Text("birthday",
                          style: TextStyle(
                            fontSize: 16.0,
                          )),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 25.0),
                      child: SizedBox(
                        width: 110.0,
                        height: 25.0,
                        child: DecoratedBox(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(12),
                            color: Colors.white,
                          ),
                          child: Center(
                            child: Text(
                              selectedDate == null
                                  ? (dropdownValueBirthday ?? birthday.first)
                                  : '${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ',
                              style: const TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.w500),
                            ),
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 15.0, top: 30.0),
                      child: SizedBox(
                        width: 88.0,
                        height: 25.0,
                        child: MaterialButton(
                          onPressed: showDatePicker,
                          shape: const StadiumBorder(),
                          color: Colors.blue,
                          child: const Text(
                            'select',
                            style: TextStyle(color: Colors.white, fontSize: 12),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                bottom: 0.0,
              ),
              child: SizedBox(
                width: 160.0,
                height: 35.0,
                child: ElevatedButton(
                    style: ButtonStyle(
                      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(18.0),
                          side: const BorderSide(
                            color: Colors.blueAccent,
                          ),
                        ),
                      ),
                    ),
                    onPressed: () {
                      _saveDataBirthday(dropdownValueBirthday!);
                      // _saveDataAgree(isChecked!);
                    },
                    child: const Text('next')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

How to solve this error and do shared Preference?

CodePudding user response:

birthday is a List<String> and Text Widget only accepts String

I believe you need to do this

selectedDate == null
        ? (dropdownValueBirthday ?? birthday.first)
        :'${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ',

Also you need to update the dropdownValueBirthday

 onDateTimeChanged: (value) {
    if (value != selectedDate) {
       setState(() {
       selectedDate = value;
       dropdownValueBirthday = '${selectedDate?.year}/${selectedDate?.month}/${selectedDate?.day} ';
         });
       }
   },

CodePudding user response:

   _saveDataBirthday(dropdownValueBirthday!.toString());

make sure this is string

  • Related