我在页面上有7行,当我再次进入同一页面时,它显示14行。

huangapple go评论54阅读模式
英文:

I have 7 rows when I out the page and go again to the same page it show 14 rows

问题

I have problem, when I run the code it works but when I out from the page and go back it show x2 for the rows its looping without any loop I make in my database 7 rows when I out and return to the same page it shows 14 rows but I have onlu 7 rows.

This is my code:

import 'dart:convert';

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

class my_ads extends StatefulWidget {
  const my_ads({Key? key}) : super(key: key);

  @override
  State<my_ads> createState() => _my_adsState();
}

List list = [];
int select_item = 0;

class _my_adsState extends State<my_ads> {
  @override
  Future MyAds() async {
    final response =
        await http.get(Uri.parse('https://***.***.***.**/getData.php'));

    if (response.statusCode == 200) {
      var red = jsonDecode(response.body);

      setState(() {
        list.addAll(red);
      });
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load Ads');
    }
  }

  @override
  void initState() {
    super.initState();

    GetData();
  }

  GetData() async {
    await MyAds();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            margin: const EdgeInsets.only(left: 70, right: 60, top: 50),
            height: 54.0,
            width: 224.0,
            child: Container(
                decoration: BoxDecoration(
                    border:
                        Border.all(color: const Color(0xffF4AC47), width: 5),
                    color: const Color(0xff42A9D2),
                    borderRadius: const BorderRadius.only(
                        bottomLeft: Radius.circular(40),
                        bottomRight: Radius.circular(40))),
                child: const Center(
                  child: Text(
                    "MyAds",
                    style: TextStyle(
                        fontSize: 25,
                        color: Color(0xff072A52),
                        fontFamily: 'Cairo'),
                    textAlign: TextAlign.center,
                  ),
                  //end logo
                )),
          ),
          const SizedBox(
            height: 35,
          ),
          Expanded(
            child: ListView.builder(
              itemCount: list.length,
              itemBuilder: ((cts, i) {
                return Column(
                  children: [
                    Container(
                      margin: const EdgeInsets.symmetric(horizontal: 10),
                      height: 180.0,
                      child: Container(
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: const Color(0xff42A9D2), width: 5),
                            borderRadius: BorderRadius.circular(8)),
                        child: Row(
                          children: [
                            const Expanded(
                              flex: 3,
                              child: Image(
                                image: AssetImage("assets/book.jpg"),
                              ),
                            ),
                            Expanded(
                              flex: 6,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "${list[i]["book_name"]}",
                                    style: TextStyle(
                                        fontSize: 20, color: Colors.black87),
                                  ),
                                  const SizedBox(height: 12),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Text("${list[i]["collage"]}"),
                                      const Icon(Icons.perm_identity_rounded)
                                    ],
                                  ),
                                  const SizedBox(height: 12),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Text("${list[i]["loc"]}"),
                                      const Icon(Icons.store)
                                    ],
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    const SizedBox(height: 16),
                  ],
                );
              }),
            ),
          ),
        ],
      ),
    );
  }
}

I tried to add this method to my PHP code $mysqli_connect-> close();

But its not working

This is my PHP code:

<?php
include("config.php");

$sql = "SELECT book_name,loc,com,spe,collage FROM ads";
$res = $connect->query($sql);

while($row = $res->fetch_assoc()) {
	$data[]=$row;
}

echo json_encode($data);

$mysqli_connect-> close();
?>
英文:

I have problem, when I run the code it works but when I out from the page and go back it show x2 for the rows its looping without any loop I make in my database 7 rows when I out and return to the same page it shows 14 rows but I have onlu 7 rows.

This is my code:

import &#39;dart:convert&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:http/http.dart&#39; as http;
class my_ads extends StatefulWidget {
const my_ads({Key? key}) : super(key: key);
@override
State&lt;my_ads&gt; createState() =&gt; _my_adsState();
}
List list = [];
int select_item = 0;
class _my_adsState extends State&lt;my_ads&gt; {
@override
Future MyAds() async {
final response =
await http.get(Uri.parse(&#39;https://***.***.***.**/getData.php&#39;));
if (response.statusCode == 200) {
var red = jsonDecode(response.body);
setState(() {
list.addAll(red);
});
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception(&#39;Failed to load Ads&#39;);
}
}
@override
void initState() {
super.initState();
GetData();
}
GetData() async {
await MyAds();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
margin: const EdgeInsets.only(left: 70, right: 60, top: 50),
height: 54.0,
width: 224.0,
child: Container(
decoration: BoxDecoration(
border:
Border.all(color: const Color(0xffF4AC47), width: 5),
color: const Color(0xff42A9D2),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40))),
child: const Center(
child: Text(
&quot;MyAds&quot;,
style: TextStyle(
fontSize: 25,
color: Color(0xff072A52),
fontFamily: &#39;Cairo&#39;),
textAlign: TextAlign.center,
),
//end logo
)),
),
const SizedBox(
height: 35,
),
Expanded(
child: ListView.builder(
itemCount: list.length,
itemBuilder: ((cts, i) {
return Column(
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 10),
height: 180.0,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: const Color(0xff42A9D2), width: 5),
borderRadius: BorderRadius.circular(8)),
child: Row(
children: [
const Expanded(
flex: 3,
child: Image(
image: AssetImage(&quot;assets/book.jpg&quot;),
),
),
Expanded(
flex: 6,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
&quot;${list[i][&quot;book_name&quot;]}&quot;,
style: TextStyle(
fontSize: 20, color: Colors.black87),
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(&quot;${list[i][&quot;collage&quot;]}&quot;),
const Icon(Icons.perm_identity_rounded)
],
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(&quot;${list[i][&quot;loc&quot;]}&quot;),
const Icon(Icons.store)
],
),
],
),
)
],
),
),
),
const SizedBox(height: 16),
],
);
}),
),
),
],
),
);
}
}

I tried to add this method to my PHP code $mysqli_connect-&gt; close();

But its not working

This is my PHP code:

&lt;?php
include(&quot;config.php&quot;);
$sql = &quot;SELECT book_name,loc,com,spe,collage FROM ads&quot;;
$res = $connect-&gt;query($sql);
while($row = $res -&gt; fetch_assoc()) {
$data[]=$row;
}
echo json_encode($data);
$mysqli_connect-&gt; close();
?&gt;

答案1

得分: 0

你在外部声明了list。即使在切换屏幕时,它仍将保持其状态。
要么你在状态小部件内声明它,要么替换:

list.addAll(red);

为:

list = red;
英文:

You declared list outside. It will maintain its state even when you change screens.
Either you declared it inside the state widget or replace :

list.addAll(red);

by :

list = red;

huangapple
  • 本文由 发表于 2023年1月9日 10:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定