Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

cant get the DropdownButton list from flutter firebase firestore

i want to do a DropdownButton that gets it's items from firebase firestore. i have a name list at firestore and i can transfer it's items to DropdownButton items can see all of names. i have 3 names at firestore and can see all of them.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:sby_cpal_demo/Helpers/AtaWidget.dart';
import 'package:url_launcher/url_launcher.dart';

class SmsGonder extends StatefulWidget {
 @override
 State<StatefulWidget> createState() {
   return SmsGonderState();
 }
}

class SmsGonderState extends State<SmsGonder> {
 List<String> _names = [];

 String title= "name0"; //*i got name0 at Firestore
 @override
 Widget build(BuildContext context) {

   return FutureBuilder(
     future: FirebaseFirestore.instance.collection("names").get(),
     builder: (context, snapshot) {
       if(snapshot.hasError){return Center(child: Text("Something went wrong"),);}
       else if(snapshot.connectionState==ConnectionState.waiting){return Center(child: Text("Names coming"),);}

       final querySnapshot = snapshot.data;
      for(int i = 0; i<2; i++){_names.add(querySnapshot.docs[i].data()["name"]);}

       return Column(
         children: <Widget>[
           DropdownButton<String>(
               focusColor: Colors.blue,
               items: _names.map((String name) {
                 return DropdownMenuItem<String>(
                   child: Text(name),
                   value: name,
                 );
               }).toList(),
               value: name0,
               onChanged: (String value) {
                 setState(() {
                   title= value;
                 });
               }),
           MaterialButton(
               child: Text(
                 "g?nder",
                 style: TextStyle(color: Colors.white),
               ),
               color: Colors.green,
               height: 20,
               onPressed: () {
                 if (MyParentWidget.of(context).userName == "?mer kalfa") {


                   print(_names); //i can print _names list with this.
                   for(int i = 0; i<2; i++){
                     debugPrint(querySnapshot.docs[i].data()["name"]);}
                  //Also i can print all firestore names at collection names one by one

                   _launchIt("sms:545465456465");
                 } else {
                   AlertDialog alertDialog = new AlertDialog(
                       title: Text("Hata: "),
                       content: Text(
                         "Mesaj g?nderme yetkiniz bulunmamaktad?r. Bu alan sadece personel taraf?ndan kullan?labilir. ",
                         style: TextStyle(fontWeight: FontWeight.bold),
                       ));
                   showDialog(context: context, builder: (_) => alertDialog);
                 }
               })
         ],
       );
     },
   );
 }

 void _launchIt(command) async {
   if (await canLaunch(command)) {
     await launch(command);
   } else {
     Text("bir sorun olu?tu.");
   }
 }
}

But if i select a name getting this error (i havent used material button yet):


════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building FutureBuilder<QuerySnapshot>(dirty, state: _FutureBuilderState<QuerySnapshot>#9a11a):
There should be exactly one item with [DropdownButton]'s value: Murat YA?AR. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

The relevant error-causing widget was: 
  FutureBuilder<QuerySnapshot> file:///C:/ornekler/sby_cpal_demo/lib/Helpers/SmsGonder.dart:22:12
When the exception was thrown, this was the stack: 
#2      new DropdownButton (package:flutter/src/material/dropdown.dart:834:15)
#3      SmsGonderState.build.<anonymous closure> (package:sby_cpal_demo/Helpers/SmsGonder.dart:33:13)
#4      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:751:55)
#5      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

i will be so glad if someone helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...