GithubHelp home page GithubHelp logo

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value this happen when i have three drop downs get from api and two of them depend of what i will choose from the first one when schoose from the three drop downs and select from the first one again got this error about dropdown_button2 HOT 12 CLOSED

ziad500 avatar ziad500 commented on June 20, 2024 1
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value this happen when i have three drop downs get from api and two of them depend of what i will choose from the first one when schoose from the three drop downs and select from the first one again got this error

from dropdown_button2.

Comments (12)

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

Duplicate of #138

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

I tried the solution you provided but the same error appears @AhmedLSayed9

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

Your code is hard to read, can you format it?

You should use:
```dart
yourCode
```

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../utils/app_colors.dart';

class CustomDropDown extends StatelessWidget {
CustomDropDown(
{super.key,
required this.list,
this.items,
this.height,
this.iconSize = 24,
required this.hint,
this.isEmpty = false,
required this.onOptionSelected,
this.iconColor,
this.onTap,
this.width,
this.title,
this.validator});

final List list;
final List<DropdownMenuItem>? items;
final String hint;
static String? selectedValue;
final void Function(OptionItem?) onOptionSelected;
final double iconSize;
final Color? iconColor;
final bool isEmpty;
final void Function()? onTap;
final double? height;
final double? width;
final String? title;
final String? Function(OptionItem?)? validator;

final TextEditingController textEditingController = TextEditingController();

@OverRide
Widget build(BuildContext context) {
return DropdownButtonFormField2(
isExpanded: true,
hint: Text(
hint,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
items: items ??
list
.map((item) => DropdownMenuItem(
value: OptionItem(
id: item.id,
title: item.title,
subtitle: item.subtitle,
description: item.description,
image: item.image),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
item.title,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
),
),
))
.toList(),
validator: validator ??
(value) {
if (value == null) {
return "please enter $title";
}
return null;
},
onChanged: (value) {
onOptionSelected(value);
},
onSaved: (value) {
selectedValue = value.toString();
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(right: 8),
),
dropdownStyleData: DropdownStyleData(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
),
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
),
);
}
}

class OptionItem extends Equatable {
final String id;
final String title;
final String? subtitle;
final String description;
final String image;

const OptionItem(
{required this.id,
required this.title,
this.description = "",
this.subtitle,
this.image = ""});

@OverRide
List<Object?> get props => [id, title, description, image, subtitle];
}

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

It still unformatted 😃

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

What should i do 😂

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

Add ```dart at the beginning your code and ``` at the end of your code

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

This file contains dropdown widget only

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

I meant formatting the code here at github

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

class CustomDropDown extends StatelessWidget {
CustomDropDown(
{super.key,
required this.list,
this.items,
this.height,
this.iconSize = 24,
required this.hint,
this.isEmpty = false,
required this.onOptionSelected,
this.iconColor,
this.onTap,
this.width,
this.title,
this.validator});

final List list;
final List? items;
final String hint;
static String? selectedValue;
final void Function(OptionItem?) onOptionSelected;
final double iconSize;
final Color? iconColor;
final bool isEmpty;
final void Function()? onTap;
final double? height;
final double? width;
final String? title;
final String? Function(OptionItem?)? validator;

final TextEditingController textEditingController = TextEditingController();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@OverRide
Widget build(BuildContext context) {
return DropdownButtonFormField2(
isExpanded: true,
hint: Text(
hint,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
items: items ??
list
.map((item) => DropdownMenuItem(
value: OptionItem(
id: item.id,
title: item.title,
subtitle: item.subtitle,
description: item.description,
image: item.image),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
item.title,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
),
),
))
.toList(),
validator: validator ??
(value) {
if (value == null) {
return "please enter $title";
}
return null;
},
onChanged: (value) {
onOptionSelected(value);
},
onSaved: (value) {
selectedValue = value.toString();
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(right: 8),
),
dropdownStyleData: DropdownStyleData(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
),
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
),
);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class OptionItem extends Equatable {
final String id;
final String title;
final String? subtitle;
final String description;
final String image;

const OptionItem(
{required this.id,
required this.title,
this.description = "",
this.subtitle,
this.image = ""});

@OverRide
List<Object?> get props => [id, title, description, image, subtitle];
} #

from dropdown_button2.

ziad500 avatar ziad500 commented on June 20, 2024

Like this 😅

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on June 20, 2024

No. Your code should be formatted like this one:

code

Anyway, You can't use String for selectedValue while using OptionItem as the value of your items.

To be compile-safe, you can annotate your DropdownButtonFormField2 with the type you want. i.e:

return  DropdownButtonFormField2< OptionItem>(
....
);

from dropdown_button2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.