English 中文(简体)
Flutter PageView not displaying all the data from database (php)
原标题:
  • 时间:2023-05-22 23:16:10
  •  标签:
  • php
  • flutter

I want to display data from database in pageview, swiping the screen like user1_name -> user1_age -> user2_name -> user2_age -> and so on for all data coming from database, but my code is showing only first user data (only one). In gridview it is showing all the data of database. I am adding the code, please help me what could be modified:

import  package:flutter/material.dart ;
import  package:flutter/src/widgets/framework.dart ;
import  package:flutter/src/widgets/placeholder.dart ;
import  package:http/http.dart  as http;
import  dart:convert ;
import  package:student/student_pages/student_detail.dart ;
import  ../utils/constants.dart ;

String website = Constants.website;

class UserDetail extends StatefulWidget {
  final String subjectId;
  const UserDetail({super.key, required this.userId});

  @override
  State<UserDetail> createState() =>
      _UserDetailState();
}

class _UserDetailState extends State<UserDetail> {
  get userId => widget.userId;

  get itemCount => null;
  Future<List> getUserData() async {
    String url =  $website/get_user_data.php ;
    var data = { user_id : userId};
    var response = await http.post(Uri.parse(url), body: data);
    var userDetail= jsonDecode(response.body.toString());
    return userDetail;
  }

  PageController pageController = PageController();
  @override
  Widget build(BuildContext context) {
      return Scaffold(
      appBar: AppBar(
        title: const Text( User Detail ),
        ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(
                padding: const EdgeInsets.all(10),
                alignment: Alignment.center,
                margin: EdgeInsets.all(10),
                width: double.infinity,
                decoration: BoxDecoration(
                color: Colors.red,
                  borderRadius:
                      BorderRadius.circular(20), //border corner radius
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.5), //color of shadow
                      spreadRadius: 5, //spread radius
                      blurRadius: 7, // blur radius
                      offset: Offset(0, 2), // changes position of shadow
                    ),
                  ],
                ),

                child: Text(
                   Swip left, right ,
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 15,
                  ),
                ),
              ),
            ),
            Expanded(
              flex: 8,
              child: FutureBuilder<List<dynamic>>(
                future: getUserData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: PageView.builder(
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          var userName = snapshot.data![index][ user_name ];
                          var userAge = snapshot.data![index][ user_age ];
                          return PageView(
                            pageSnapping: true,
                            controller: pageController,
                            children: [
                              Container(
                                child: Text(
                                  "$index:- $userName,",
                                ),
                              ),
                              Container(
                                child: Text(
                                   $userAge ,
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    );
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            ),


           
          ],
        ),
      ),
    );
  }
}

问题回答

I didn t see it at first, but you are returning another PageView widget inside your PageView.builder widget. It s not good practice to nest PageView widgets inside each other because it will lead to scrolling complications.

Here is the corrected code.

import  package:flutter/material.dart ;
import  package:flutter/src/widgets/framework.dart ;
import  package:flutter/src/widgets/placeholder.dart ;
import  package:http/http.dart  as http;
import  dart:convert ;
import  package:student/student_pages/student_detail.dart ;
import  ../utils/constants.dart ;

String website = Constants.website;

class UserDetail extends StatefulWidget {
  final String subjectId;
  const UserDetail({super.key, required this.userId});

  @override
  State<UserDetail> createState() => _UserDetailState();
}

class _UserDetailState extends State<UserDetail> {
  get userId => widget.userId;

  get itemCount => null;
  Future<List> getUserData() async {
    String url =  $website/get_user_data.php ;
    var data = { user_id : userId};
    var response = await http.post(Uri.parse(url), body: data);
    var userDetail = jsonDecode(response.body.toString());
    return userDetail;
  }

  PageController pageController = PageController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text( User Detail ),
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(
                padding: const EdgeInsets.all(10),
                alignment: Alignment.center,
                margin: EdgeInsets.all(10),
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(20), //border corner radius
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.5), //color of shadow
                      spreadRadius: 5, //spread radius
                      blurRadius: 7, // blur radius
                      offset: Offset(0, 2), // changes position of shadow
                    ),
                  ],
                ),
                child: Text(
                   Swip left, right ,
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 15,
                  ),
                ),
              ),
            ),
            Expanded(
              flex: 8,
              child: FutureBuilder<List<dynamic>>(
                future: getUserData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: PageView.builder(
                        controller: pageController, //use pageController here
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          var userName = snapshot.data![index][ user_name ];
                          var userAge = snapshot.data![index][ user_age ];
                          return Column( //don t return another PageView
                            children: [
                              Container(
                                child: Text(
                                  "$index:- $userName,",
                                ),
                              ),
                              Container(
                                child: Text(
                                   $userAge ,
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    );
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}






相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签