Good noon every one i want to ask on how to put a dropdown onchanged i am using sqllite for my database.
error encounter [{ "resource": "/c:/Users/VIMS-SERVER/Documents/Flutter Projects/apps/lib/widget/note_form_widget.dart", "owner": "generated_diagnostic_collection_name#0", "code": { "value": "undefined_method", "target": { "$mid": 1, "external": "https://dart.dev/diagnostics/undefined_method", "path": "/diagnostics/undefined_method", "scheme": "https", "authority": "dart.dev" } }, "severity": 8, "message": "The method 'setState' isn't defined for the type 'NoteFormWidget'.\nTry correcting the name to the name of an existing method, or defining a method named 'setState'.", "source": "dart", "startLineNumber": 228, "startColumn": 9, "endLineNumber": 228, "endColumn": 17 }]
[{ "resource": "/c:/Users/VIMS-SERVER/Documents/Flutter Projects/apps/lib/widget/note_form_widget.dart", "owner": "generated_diagnostic_collection_name#0", "code": { "value": "undefined_identifier", "target": { "$mid": 1, "external": "https://dart.dev/diagnostics/undefined_identifier", "path": "/diagnostics/undefined_identifier", "scheme": "https", "authority": "dart.dev" } }, "severity": 8, "message": "Undefined name 'dropdownValue'.\nTry correcting the name to one that is defined, or defining the name.", "source": "dart", "startLineNumber": 229, "startColumn": 11, "endLineNumber": 229, "endColumn": 24 }]
[{ "resource": "/c:/Users/VIMS-SERVER/Documents/Flutter Projects/apps/lib/widget/note_form_widget.dart", "owner": "generated_diagnostic_collection_name#0", "code": { "value": "undefined_identifier", "target": { "$mid": 1, "external": "https://dart.dev/diagnostics/undefined_identifier", "path": "/diagnostics/undefined_identifier", "scheme": "https", "authority": "dart.dev" } }, "severity": 8, "message": "Undefined name 'newValue'.\nTry correcting the name to one that is defined, or defining the name.", "source": "dart", "startLineNumber": 229, "startColumn": 27, "endLineNumber": 229, "endColumn": 35 }]
edit_note_page.dart
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
actions: [buildButton()],
),
body: Form(
key: _formKey,
child: NoteFormWidget(
category: category,
onChangedCategory: (category) => setState(() => this.category = category),
),
),
);
note_form_widget.dart
Widget buildCategory() => DropdownButton(
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>['A1', 'A2', 'A3', 'A4','A5','C1']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: category,
child: Text(value),
);
}).toList(), onChanged: (String? value)
{
setState(() {
dropdownValue = newValue!;//im getting an error on this part
});
},
error encounter
The method 'setState' isn't defined for the type 'NoteFormWidget'.
Try correcting the name to the name of an existing method, or defining a method named 'setState'.
[{
"resource": "/c:/Users/VIMS-SERVER/Documents/Flutter Projects/apps/lib/widget/note_form_widget.dart",
"owner": "_generated_diagnostic_collection_name_#0",
"code": {
"value": "undefined_identifier",
"target": {
"$mid": 1,
"external": "https://dart.dev/diagnostics/undefined_identifier",
"path": "/diagnostics/undefined_identifier",
"scheme": "https",
"authority": "dart.dev"
}
},
"severity": 8,
"message": "Undefined name 'dropdownValue'.\nTry correcting the name to one that is defined, or defining the name.",
"source": "dart",
"startLineNumber": 229,
"startColumn": 11,
"endLineNumber": 229,
"endColumn": 24
}]
[{
"resource": "/c:/Users/VIMS-SERVER/Documents/Flutter Projects/apps/lib/widget/note_form_widget.dart",
"owner": "_generated_diagnostic_collection_name_#0",
"code": {
"value": "undefined_identifier",
"target": {
"$mid": 1,
"external": "https://dart.dev/diagnostics/undefined_identifier",
"path": "/diagnostics/undefined_identifier",
"scheme": "https",
"authority": "dart.dev"
}
},
"message": "Undefined name 'newValue'.\nTry correcting the name to one that is defined, or defining the name.",
}]
CodePudding user response:
The setState
method doesn't work on StatelessWidget
because StatelessWidget
doesn't have a state. Only StatefulWidget
has a state, and therefore only it has a setState
.
So you most turn your StatelessWidget
to StatefulWidget
.
CodePudding user response:
I think your note_widget is stateless so you change to the stateful widget. you cant use a set state callback on the stateless widget.Dartpad live check here.you can use another state management tool(Provider ,cubit,riverport,bloc) to update your value
DropdownButton(
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>['A1', 'A2', 'A3', 'A4', 'A5', 'C1']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
value: dropdownvalu,
onChanged: (values) {
setState(() {
dropdownvalu = values.toString();
});
},
)
Widget
class _ParentState extends State<Parent> {
Widget build(context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
setState(() {
counter ;
});
},
icon: const Icon(Icons.add),
),
],
),
body: Column(
children: [
DropdownButton(
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>['A1', 'A2', 'A3', 'A4', 'A5', 'C1']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
value: dropdownvalu,
onChanged: (values) {
setState(() {
dropdownvalu = values.toString();
});
},
),
Center(
child: Text(
"$dropdownvalu",
style: TextStyle(fontSize: 30),
)),
],
),
);
}
}
SampleCode
import 'package:flutter/material.dart';
//import 'package:pucon/home.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Parent(),
);
}
}
class Parent extends StatefulWidget {
@override
State<Parent> createState() => _ParentState();
}
String dropdownvalu = "A1";
class _ParentState extends State<Parent> {
Widget build(context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
setState(() {
counter ;
});
},
icon: const Icon(Icons.add),
),
],
),
body: Column(
children: [
DropdownButton(
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>['A1', 'A2', 'A3', 'A4', 'A5', 'C1']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
value: dropdownvalu,
onChanged: (values) {
setState(() {
dropdownvalu = values.toString();
});
},
),
Center(
child: Text(
"$dropdownvalu",
style: TextStyle(fontSize: 30),
)),
],
),
);
}
}
int counter = 0;
class Child extends StatefulWidget {
Child({Key? key}) : super(key: key);
@override
_ChildState createState() => _ChildState();
}
class _ChildState extends State<Child> {
@override
Widget build(BuildContext context) {
return Center(
child: Text(
"$dropdownvalu",
style: TextStyle(fontSize: 30),
));
} //to be incremented when the parent's button is clicked on.
}
CodePudding user response:
// dbHelper
Future<List> getEmployeeData() async {
try {
final dbClient = await db;
var l = await dbClient!.transaction(
(txn) async => txn.rawQuery("SELECT * FROM ${DbConstants.tblGloEmployeeMaster} where is_active = 1"),
);
return l;
} catch (e) {
return [];
}
}
// getx
// login controller
// init
final employeeModelList = List<EmployeeModel>.empty(growable: true).obs;
// create method
void _getEmployees() async {
List list = await _dbHelper.getEmployeeData();
employeeModelList.value = [];
employeeModelList.value = list.map((employeeModel) => EmployeeModel.fromJson(employeeModel)).toList();
}
// create model
class EmployeeModel {
EmployeeModel({
required this.employeeId,
required this.employeeNm,
});
late final int employeeId;
late final String employeeNm;
EmployeeModel.fromJson(Map<String, dynamic> json) {
employeeId = json['employee_id'];
employeeNm = json['employee_nm'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['employee_id'] = employeeId;
_data['employee_nm'] = employeeNm;
return _data;
}
}
// stateless widget
// call
Container(
child: _searchEmployee(context),
),
// method
Widget _searchEmployee(BuildContext context) {
return Obx(
() => CustomSearchableDropDown(
items: _loginController.employeeModelList,
label: Strings.selectEmployee,
labelStyle: Theme.of(context).textTheme.bodyText1!.merge(TextStyles.defaultRegular),
dropdownLabelStyle: Theme.of(context).textTheme.bodyText1!.merge(TextStyles.defaultRegular),
dropDownMenuItems: _loginController.employeeModelList.map((item) {
return "${item.employeeId} - ${item.employeeNm}"; // ui view
}).toList(),
suffixIcon:
Icon(Icons.arrow_drop_down, size: Sizes.s16, color: Get.isDarkMode ? AppColors.white : AppColors.black),
decoration: BoxDecoration(border: Border(bottom: BorderSide(color: AppColors.fontGray))),
onChanged: (value) {
if (value != null) {
EmployeeModel employeeModel = value;
_loginController.employeeId.value = int.parse(employeeModel.employeeId);
_loginController.employeeName.value = employeeModel.employeeNm;
}
},
),
);
}