Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在sqflite列的逗号分隔值的颤振中进行下拉_Flutter_Dart_Dropdown_Sqflite - Fatal编程技术网

Flutter 如何在sqflite列的逗号分隔值的颤振中进行下拉

Flutter 如何在sqflite列的逗号分隔值的颤振中进行下拉,flutter,dart,dropdown,sqflite,Flutter,Dart,Dropdown,Sqflite,我想在颤振下拉哪个值来自sqflite数据库。样本值为 { "id": 5, "name": "New", "stages": "{\"Lead in\":false,\"Contacted\":false,\"Demo Presented\":false,\"Proposal Sent\":false,\"t4eg4ehhhe4h\":false,\"Extra Features Discussed\":false

我想在颤振下拉哪个值来自sqflite数据库。样本值为

    {
            "id": 5,
            "name": "New",
            "stages": "{\"Lead in\":false,\"Contacted\":false,\"Demo Presented\":false,\"Proposal Sent\":false,\"t4eg4ehhhe4h\":false,\"Extra Features Discussed\":false,\"Negotiation\":false,\"Closure\":false}",
            "created_at": "2019-05-03 17:49:05",
            "updated_at": "2019-06-06 16:16:04",
            "deleted_at": null
        },
我需要像引入,接触,演示提出,建议和下面是我的代码阶段下拉列表

                              FutureBuilder(
                              future: pipelineHelper.getPipeStage(pipelineId),
                              builder: (BuildContext context,
                                  AsyncSnapshot<List<Pipeline>>
                                  snapshot) {
                                if (!snapshot.hasData)
                                  return CircularProgressIndicator();
                                return DropdownButton(
                                  items: snapshot.data
                                      .map((stageList) => DropdownMenuItem<Pipeline>(
                                    value: stageList, child: Text(stageList.stages),))
                                      .toList(),
                                  onChanged: (actList) {
                                    setState(() {
                                      stageName = actList.title;
                                      debugPrint('stage selected');
                                    });
                                  },
                                  isExpanded: true,
                                );
                              })

如果我明白你的意思
在代码pipelineHelper.getPipeStage(pipelineId)中,只返回一条管道记录
因此,AsyncSnapshot列表管道应更改为AsyncSnapshot管道
在项目中,可以使用拆分来拆分逗号并返回DropdownMenuItem

items: snapshot.data.stage
                    .split(', ')
完整代码

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

// To parse this JSON data, do
//
//     final stages = stagesFromJson(jsonString);

List<Pipeline> stagesFromJson(String str) =>
    List<Pipeline>.from(json.decode(str).map((x) => Pipeline.fromJson(x)));

String stagesToJson(List<Pipeline> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Pipeline {
  String pipelineId;
  String stage;

  Pipeline({
    this.pipelineId,
    this.stage,
  });

  factory Pipeline.fromJson(Map<String, dynamic> json) => Pipeline(
        pipelineId: json["pipelineId"],
        stage: json["stage"],
      );

  Map<String, dynamic> toJson() => {
        "pipelineId": pipelineId,
        "stage": stage,
      };
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: JsonApiDropdown(),
    );
  }
}

class JsonApiDropdown extends StatefulWidget {
  @override
  JsonApiDropdownState createState() {
    return new JsonApiDropdownState();
  }
}

class JsonApiDropdownState extends State<JsonApiDropdown> {
  String _currentSelection;

  final String uri = 'https://jsonplaceholder.typicode.com/users';

  Future<Pipeline> _fetchstage() async {
    String jsonString =
        '[ { "pipelineId": "CDG0008", "stage": "stage1, stage2, stage3" }, { "pipelineId": "CDG0004", "stage": "Ceo - Chief Executive Officer" } ]';
    final stages = stagesFromJson(jsonString);
    return stages[0];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Fetching data from JSON - DropdownButton'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            FutureBuilder<Pipeline>(
                future: _fetchstage(),
                builder:
                    (BuildContext context, AsyncSnapshot<Pipeline> snapshot) {
                  if (!snapshot.hasData) return CircularProgressIndicator();
                  return DropdownButton<String>(
                    items: snapshot.data.stage
                        .split(', ')
                        .map((data) => DropdownMenuItem<String>(
                              child: Text(data),
                              value: data,
                            ))
                        .toList(),
                    onChanged: (String value) {
                      setState(() {
                        _currentSelection = value;
                      });
                    },
                    isExpanded: false,
                    //value: _currentUser,
                    hint: Text('Select User'),
                  );
                }),
            SizedBox(height: 20.0),
            _currentSelection != null
                ? Text("selection : " + _currentSelection)
                : Text("No selected"),
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“dart:convert”;
将“package:http/http.dart”导入为http;
void main()=>runApp(MyApp());
//要解析此JSON数据,请执行以下操作
//
//最终阶段=stagesFromJson(jsonString);
List stagesFromJson(字符串str)=>
List.from(json.decode(str.map)(x)=>Pipeline.fromJson(x));
字符串stagesToJson(列表数据)=>
encode(List.from(data.map((x)=>x.toJson());
类管道{
字符串管道ID;
弦乐台;
管道({
这是pipelineId,
这个阶段,,
});
工厂管道.fromJson(映射json)=>管道(
pipelineId:json[“pipelineId”],
stage:json[“stage”],
);
映射到JSON()=>{
“管道ID”:管道ID,
“舞台”:舞台,
};
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
//这是应用程序的主题。
//
//尝试使用“flutter run”运行应用程序。您将看到
//应用程序有一个蓝色工具栏。然后,在不退出应用程序的情况下,重试
//将下面的primarySwatch更改为Colors.green,然后调用
//“热重新加载”(在运行“颤振运行”的控制台中按“r”,
//或者只需将更改保存到颤振IDE中的“热重新加载”。
//请注意,计数器没有重置回零;应用程序
//未重新启动。
主样本:颜色。蓝色,
),
主页:JsonApiDropdown(),
);
}
}
类JsonApiDropdown扩展了StatefulWidget{
@凌驾
JsonApiDropdownState createState(){
返回新的JsonApiDropdownState();
}
}
类JsonApiDropdownState扩展状态{
字符串currentSelection;
最后一个字符串uri=https://jsonplaceholder.typicode.com/users';
Future _fetchstage()异步{
字符串jsonString=
“[{”pipelineId:“CDG0008”,“stage:“stage1,stage2,stage3”},{”pipelineId:“CDG0004”,“stage:“Ceo-首席执行官”}”;
最终阶段=stagesFromJson(jsonString);
返回阶段[0];
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:Text('fetchingdatafromsjson-DropdownButton'),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
mainAxisSize:mainAxisSize.max,
儿童:[
未来建设者(
未来:_fetchstage(),
建设者:
(BuildContext上下文,异步快照){
如果(!snapshot.hasData)返回CircularProgressIndicator();
返回下拉按钮(
项目:snapshot.data.stage
.split(“,”)
.map((数据)=>下拉菜单项(
子项:文本(数据),
价值:数据,
))
.toList(),
onChanged:(字符串值){
设置状态(){
_当前选择=值;
});
},
isExpanded:错,
//值:_currentUser,
提示:文本(“选择用户”),
);
}),
尺寸箱(高度:20.0),
_currentSelection!=null
文本(“选择:”+\u当前选择)
:文本(“未选择”),
],
),
),
);
}
}

我在添加snapshot.data.stage.split(“,”)时遇到此错误。没有为类“List”定义getter“stages”。我们有不同的字段名,我使用stage和您的is stages。它们完全是两个不同的问题。我建议在一个新的问题上贴一个帖子。对某人来说,搜索和帮助会更容易。谢谢。谢谢你的帮助。如果你有兴趣回答我的新问题,你可以访问
items: snapshot.data.stage
                    .split(', ')
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

// To parse this JSON data, do
//
//     final stages = stagesFromJson(jsonString);

List<Pipeline> stagesFromJson(String str) =>
    List<Pipeline>.from(json.decode(str).map((x) => Pipeline.fromJson(x)));

String stagesToJson(List<Pipeline> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Pipeline {
  String pipelineId;
  String stage;

  Pipeline({
    this.pipelineId,
    this.stage,
  });

  factory Pipeline.fromJson(Map<String, dynamic> json) => Pipeline(
        pipelineId: json["pipelineId"],
        stage: json["stage"],
      );

  Map<String, dynamic> toJson() => {
        "pipelineId": pipelineId,
        "stage": stage,
      };
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: JsonApiDropdown(),
    );
  }
}

class JsonApiDropdown extends StatefulWidget {
  @override
  JsonApiDropdownState createState() {
    return new JsonApiDropdownState();
  }
}

class JsonApiDropdownState extends State<JsonApiDropdown> {
  String _currentSelection;

  final String uri = 'https://jsonplaceholder.typicode.com/users';

  Future<Pipeline> _fetchstage() async {
    String jsonString =
        '[ { "pipelineId": "CDG0008", "stage": "stage1, stage2, stage3" }, { "pipelineId": "CDG0004", "stage": "Ceo - Chief Executive Officer" } ]';
    final stages = stagesFromJson(jsonString);
    return stages[0];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Fetching data from JSON - DropdownButton'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            FutureBuilder<Pipeline>(
                future: _fetchstage(),
                builder:
                    (BuildContext context, AsyncSnapshot<Pipeline> snapshot) {
                  if (!snapshot.hasData) return CircularProgressIndicator();
                  return DropdownButton<String>(
                    items: snapshot.data.stage
                        .split(', ')
                        .map((data) => DropdownMenuItem<String>(
                              child: Text(data),
                              value: data,
                            ))
                        .toList(),
                    onChanged: (String value) {
                      setState(() {
                        _currentSelection = value;
                      });
                    },
                    isExpanded: false,
                    //value: _currentUser,
                    hint: Text('Select User'),
                  );
                }),
            SizedBox(height: 20.0),
            _currentSelection != null
                ? Text("selection : " + _currentSelection)
                : Text("No selected"),
          ],
        ),
      ),
    );
  }
}