首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取mysql数据库值

获取mysql数据库值
EN

Stack Overflow用户
提问于 2021-03-15 20:30:18
回答 2查看 534关注 0票数 1

我正在尝试从mysql数据库中获取值到我的颤振项目(这些是使用分页的)。我正在添加自定义数据及其工作。但无法获取数据库值

代码语言:javascript
运行
复制
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:http/http.dart'as http;

class AllQuestion extends StatefulWidget {
 String fldPaperTypeID;
 AllQuestion({this.fldPaperTypeID});
@override
_AllQuestionState createState() => _AllQuestionState();
}
List<Question> _question;
List<Question> populateData;

QuestionDataSource _questionDataSource = QuestionDataSource();
List<Question> paginatedDataSource = [];

class _AllQuestionState extends State<AllQuestion> {
bool showLoadingIndicator = true;

Future<List> GetAllQuestion() async{

final response = await http.post('http://eduapp.studysmile.lk/getquestion',body: {"fldPaperTypeID":widget.fldPaperTypeID});
return json.decode(response.body);
}
@override
void initState() {
// TODO: implement initState
super.initState();
_question = populateData();
}
@override

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Question'),
  ),
  body: LayoutBuilder(
    builder: (context, constraints) {
      return Row(children: [
        Column(
          children: [
            SizedBox(
                height: constraints.maxHeight - 60,
                width: constraints.maxWidth,
                child: buildStack(constraints)),
            Container(
              height: 60,
              width: constraints.maxWidth,
              child: SfDataPager(
                rowsPerPage: 1,
                direction: Axis.horizontal,
                onPageNavigationStart: (int pageIndex) {
                  setState(() {
                    showLoadingIndicator = true;
                  });
                },
                delegate: _questionDataSource,
                onPageNavigationEnd: (int pageIndex) {
                  setState(() {
                    showLoadingIndicator = false;
                  });
                },
              ),
            )
          ],
        ),
      ]);
    },
  ),
);
}

Widget buildDataGrid(BoxConstraints constraint) {
return SfDataGrid(
    source: _questionDataSource,
    columnWidthMode: ColumnWidthMode.fill,
    columns: <GridColumn>[
      GridNumericColumn(mappingName: 'id', headerText: 'ID'),
      GridTextColumn(mappingName: 'name', headerText: 'Name'),
      GridNumericColumn(mappingName: 'salaryS', headerText: 'Salary'),
    ]);
}

Widget buildStack(BoxConstraints constraints) {
List<Widget> _getChildren() {
  final List<Widget> stackChildren = [];
  stackChildren.add(buildDataGrid(constraints));

  if (showLoadingIndicator) {
    stackChildren.add(Container(
      color: Colors.black12,
      width: constraints.maxWidth,
      height: constraints.maxHeight,
      child: Align(
        alignment: Alignment.center,
        child: CircularProgressIndicator(
          strokeWidth: 3,
        ),
      ),
    ));
  }

  return stackChildren;
}

return Stack(
  children: _getChildren(),
);
}
 /*  List<Question> populateData() {
return [
  Question(10001, 'James', 20000),
  Question(10002, 'Kathryn', 30000),
  Question(10001, 'James', 20000),
  Question(10002, 'Kathryn', 30000),
  Question(10001, 'James', 20000),
  Question(10002, 'Kathryn', 30000),
  Question(10001, 'James', 20000),
  Question(10002, 'Kathryn', 30000),
  Question(10001, 'James', 20000),
  Question(10002, 'Kathryn', 30000),
];
}*/
Future<List<Question>> populateData()async{
final response = await http.post('http://eduapp.studysmile.lk/getquestion',body: 
{"fldPaperTypeID":widget.fldPaperTypeID});
List<Question> populateData = json.decode(response.body);
return populateData;


 }
}
/*
 class SendallQuestion extends StatelessWidget {

List list;
SendallQuestion({this.list});
@override
Widget build(BuildContext context) {
return  ListView.builder(
    itemCount: list == null?0:list.length,
    itemBuilder: (ctx,i){
      return Container(
        alignment: Alignment.center,
        margin: EdgeInsets.all(20),
        child: FlatButton(
          child: Align(
              alignment: Alignment(0.2, 0.6),
              child: Text(list[i]['fldQuestion'], style: TextStyle(fontSize: 20.0),)
          ),
          color: Color(0xff7c4dff),
          textColor: Colors.white,
          onPressed: (){

          },


        ),
      );

    }
);
 }
}*/

class Question{

final int fldQuestionID;
final int fldPaperTypeID;
final String fldQuestion;
Question(this.fldPaperTypeID,this.fldQuestion,this.fldQuestionID);
}

class QuestionDataSource extends DataGridSource<Question> {
@override
List<Question> get dataSource => paginatedDataSource;
@override
Object getValue(Question question, String columnName) {
switch (columnName) {
  case 'id':
    return question.fldPaperTypeID;
    break;
  case 'name':
    return question.fldQuestionID;
    break;
  case 'salaryS':
    return question.fldQuestion;
    break;
  default:
    return ' ';
    break;
}
}

@override
int get rowCount => _question.length;

@override
Future<bool> handlePageChange(int oldPageIndex, int newPageIndex,
  int startRowIndex, int rowsPerPage) async {
int endIndex = startRowIndex + rowsPerPage;
if (endIndex > _question.length) {
  endIndex = _question.length - 1;
}

await Future.delayed(Duration(milliseconds: 2000));

paginatedDataSource = List.from(
    _question.getRange(startRowIndex, endIndex).toList(growable: false));
notifyListeners();
return true;
 }
}

这是我的错误消息**lib/all质询view.dart:42:17: Error:类型'Future‘的值不能分配给'List’类型的变量。

'dart:async'.

  • 'List‘的
  • ’未来‘来自'dart:core'.
  • 'Question’,来自‘package:Study微笑/all质询view.dart’(lib/allaskview.dart)。_question = populateData();^**
EN

回答 2

Stack Overflow用户

发布于 2021-03-15 20:49:19

在你的情况下,我想这会起作用的:

代码语言:javascript
运行
复制
List _question = List();

Future GetAllQuestion() async{

final response = await http.post('http://eduapp.studysmile.lk/getquestion',body: {"fldPaperTypeID":widget.fldPaperTypeID});
    var data = json.decode(response.body);
    setState(() {
      _question= data;
    });
}
void initState() {
// TODO: implement initState
super.initState();
this.GetAllQuestion();
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-16 12:15:49

试试这段代码。它起作用了。

代码语言:javascript
运行
复制
import 'package:http/http.dart' as http;
import 'package:json_helpers/json_helpers.dart';

Future main() async {
  final list = await populateData();
  print(list[0].fldQuestion);
}

Future<List<Question>> populateData() async {
  final typeID = '1';
  final response = await http.post(
      Uri.parse('http://eduapp.studysmile.lk/getquestion'),
      body: {'fldPaperTypeID': typeID});
  if (response.statusCode == 200) {
    return response.body.jsonList((e) => Question.fromJson(e));
  } else {
    throw 'Bad response';
  }
}

class Question {
  final String fldQuestionID;
  final String fldPaperTypeID;
  final String fldQuestion;

  Question({this.fldPaperTypeID, this.fldQuestion, this.fldQuestionID});

  factory Question.fromJson(Map<String, dynamic> json) {
    return Question(
        fldPaperTypeID: json['fldPaperTypeID'] as String,
        fldQuestion: json['fldQuestion'] as String,
        fldQuestionID: json['fldQuestionID'] as String);
  }
}

输出:

代码语言:javascript
运行
复制
1. දත්ත සැකසීමට පෙර වලංගුතාවය පරික්ෂා කිරීමේදී අනුගමනය කළ යුතු වඩාත් නිවැරදි  අනුපිළිවෙල  වන්නේ
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66645238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档