php 使用flutter mysql缓存数据

8ehkhllq  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(100)

我在应用程序中使用论坛系统。我缓存类别没有问题,但我有问题的职位,因为当上市的职位,我发送“限制”和“类别”信息到php页面。
我在下面分享我的邮政编码。

我收到这些代码的帖子

List<ForumCategoryModel> fromJson(String jsonString) {
    final data = json.decode(jsonString);
    return List<ForumCategoryModel>.from(data.map((item) => ForumCategoryModel.fromJson(item)));
  }

  Future<List<ForumCategoryModel>> getModel() async {
    var formData = FormData.fromMap({
      "category": widget.category,
      "limit": limit,
    });

    Response response = await dio.get(
      view_url,
      queryParameters: {
        "category": widget.category.toString(),
        "limit": limit.toString(),
      },
      options: buildCacheOptions(const Duration(days: 7), 
          maxStale: const Duration(days: 10),
          forceRefresh: true),
    );
    if (response.statusCode == 200) {
      List<ForumCategoryModel> list = fromJson(response.data);
      return list;
    } else {
      return [];
    }
  }

我用这些代码获取帖子所有者的信息

Future getusr(String uid) async {
    Response response = await dio.get(
      user_info,
      queryParameters: {"user_id": uid.toString()},
      options: buildCacheOptions(Duration(days: 7),
          maxStale: Duration(days: 10),
          forceRefresh: true),
    );

    return json.decode(response.data);
  }

在本节中,我列出了我所获取的数据。

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

  getlist() async {
    cagetoryList = await getModel();
    setState(() {
      loading = true;
    });
  }

Expanded(
                        child: AnimationLimiter(
                          child: ListView.builder(
                            itemCount: cagetoryList.length,
                            itemBuilder: (context, index) {
                              ForumCategoryModel categoryModels = cagetoryList[index];
                              return AnimationConfiguration.staggeredList(
                                position: index,
                                duration: const Duration(milliseconds: 400),
                                child: SlideAnimation(
                                  verticalOffset: 50.0,
                                  child: FadeInAnimation(
                                    child: GestureDetector(
                                      onTap: () {
                                        print(categoryModels.post_id);
                                      },
                                      child: Padding(
                                        padding: const EdgeInsets.only(
                                            top: 3.0, bottom: 3.0, right: 5.0, left: 5.0),
                                        child: Container(
                                          height: 120.h,
                                          width: MediaQuery.of(context).size.width,
                                          decoration: BoxDecoration(
                                            boxShadow: [
                                              BoxShadow(
                                                color: Colors.black54.withOpacity(0.1),
                                                spreadRadius: 2,
                                                blurRadius: 2,
                                                offset: const Offset(2.5, 2.5),
                                              ),
                                            ],
                                            color: Colors.brown.withOpacity(0.1),
                                            borderRadius: BorderRadius.circular(5),
                                          ),
                                          child: Padding(
                                            padding: const EdgeInsets.all(8.0),
                                            child: Column(
                                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                SizedBox(
                                                  height: 40.h,
                                                  child: FutureBuilder(
                                                    future:
                                                        getusr(categoryModels.user_id.toString()),
                                                    builder: (context, asyncSnapshot) {
                                                      if (asyncSnapshot.hasData) {
                                                        List list = asyncSnapshot.data;
                                                        print(list[0]["username"].toString());
                                                        return Row(
                                                          children: [
                                                            InkWell(
                                                              onTap: () {
                                                                print(
                                                                    "User id: ${list[0]["user_id"].toString()} ");
                                                              },
                                                              child: CircleAvatar(
                                                                radius: 20.sp,
                                                                backgroundColor:
                                                                    Colors.grey.shade800,
                                                                backgroundImage:
                                                                    CachedNetworkImageProvider(
                                                                  list[0]["images"].toString(),
                                                                ),
                                                              ),
                                                            ),
                                                            SizedBox(
                                                              width: 15.w,
                                                            ),
                                                            Column(
                                                              crossAxisAlignment:
                                                                  CrossAxisAlignment.start,
                                                              mainAxisAlignment:
                                                                  MainAxisAlignment.start,
                                                              children: [
                                                                Text(
                                                                  textScaleFactor: 1,
                                                                  "${list[0]["username"].toString()}",
                                                                  style: TextStyle(
                                                                      fontSize: 15.sp,
                                                                      color: Colors.red,
                                                                      fontWeight: FontWeight.bold),
                                                                ),
                                                                Text(
                                                                  textScaleFactor: 1,
                                                                  "text",
                                                                  style: TextStyle(
                                                                      fontSize: 10.sp,
                                                                      color: HexColor("ababab"),
                                                                      fontWeight: FontWeight.w400),
                                                                ),
                                                              ],
                                                            ),
                                                          ],
                                                        );
                                                      }
                                                      return const Center(
                                                          child: CircularProgressIndicator(
                                                              valueColor:
                                                                  AlwaysStoppedAnimation<Color>(
                                                                      Colors.white)));
                                                    },
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 5.h,
                                                ),
                                                Flexible(
                                                  child: RichText(
                                                    textScaleFactor: 1,
                                                    overflow: TextOverflow.ellipsis,
                                                    strutStyle: StrutStyle(fontSize: 10.0.sp),
                                                    text: TextSpan(
                                                      text: categoryModels.title,
                                                      style: TextStyle(
                                                          fontSize: 15.sp,
                                                          color: HexColor("#B87652"),
                                                          fontWeight: FontWeight.w600),
                                                    ),
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 5.h,
                                                ),
                                                Flexible(
                                                  child: RichText(
                                                    textScaleFactor: 1,
                                                    maxLines: 1,
                                                    overflow: TextOverflow.ellipsis,
                                                    text: TextSpan(
                                                      text: categoryModels.body,
                                                      style: TextStyle(
                                                          color: Colors.grey.withOpacity(0.5),
                                                          fontSize: 13.sp),
                                                    ),
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 5.h,
                                                ),
                                                Row(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: [
                                                    Row(
                                                      children: [
                                                        InkWell(
                                                          onTap: () {},
                                                          child: Icon(
                                                            CupertinoIcons.heart,
                                                            size: 20.sp,
                                                            color: HexColor("#B87652"),
                                                          ),
                                                        ),
                                                        SizedBox(
                                                          width: 5.w,
                                                        ),
                                                        Text(
                                                          textScaleFactor: 1,
                                                          categoryModels.comment_count,
                                                          style: TextStyle(
                                                              fontSize: 12.sp,
                                                              color: HexColor("ababab")),
                                                        ),
                                                        SizedBox(
                                                          width: 10.w,
                                                        ),
                                                        InkWell(
                                                          onTap: () {},
                                                          child: Icon(
                                                            CupertinoIcons.bubble_middle_bottom,
                                                            size: 20.sp,
                                                            color: HexColor("#B87652"),
                                                          ),
                                                        ),
                                                        SizedBox(
                                                          width: 5.w,
                                                        ),
                                                        Text(
                                                          textScaleFactor: 1,
                                                          categoryModels.comment_count,
                                                          style: TextStyle(
                                                              fontSize: 12.sp,
                                                              color: HexColor("ababab")),
                                                        ),
                                                      ],
                                                    ),
                                                    Text(
                                                      textScaleFactor: 1,
                                                      categoryModels.creatAt.substring(0, 10),
                                                      style: TextStyle(
                                                        fontSize: 10.sp,
                                                        color: HexColor("ababab"),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            },
                          ),
                        ),
                      ),

因此,我在列表中显示帖子,在帖子中显示用户图片和姓名。
我在第一次打开的时候拍了10张照片。
我想在没有互联网的时候给他们看。

ctrmrzij

ctrmrzij1#

我有一个好消息,我找到了自己的答案:)
下面,我把我提取的数据和我写的附加代码的编辑版本。
任何需要的人都可以使用它:)

Future<List<ForumCategoryModel>> getModel() async {
try {
  Response response = await dio.get(
    view_url,
    queryParameters: {
      "category": widget.category.toString(),
      "limit": limit.toString(),
    },
    options: buildCacheOptions(const Duration(days: 7),
        maxStale: const Duration(days: 10), forceRefresh: true),
  );
  saveList(response.data);
  print("datam : ${response.data}");
  List<ForumCategoryModel> list = fromJson(response.data);
  return list;
} catch (e) {
  List<ForumCategoryModel> str = await getListem();
  return str;
}

}

Future getusr(String uid) async {
try {
  Response response = await dio.get(
    user_info,
    queryParameters: {"user_id": uid.toString()},
    options: buildCacheOptions(const Duration(days: 7),
        maxStale: const Duration(days: 10), forceRefresh: true),
  );
  saveUserCache(uid, response.data);
  print("user data : ${response.data}");
  return json.decode(response.data);
} catch (e) {
  String data = await getUserCache(uid);
  print(data);
  return json.decode(data);
}

}
这些是我保存和提取记录的代码。前两个代码是用于帖子的-其他两个代码是用于用户信息的。

saveList(String kayit) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(widget.category, kayit);

}

Future<List<ForumCategoryModel>> getListem() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? liste = prefs.getString(widget.category);

List<ForumCategoryModel> list = fromJson(liste.toString());
return list;

}

saveUserCache(String Uid, String data) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(Uid, data);

}

getUserCache(String Uid) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? liste = prefs.getString(Uid);

return liste;

}
当互联网可用时,它会保存最后10个帖子。
当没有互联网时,它会显示最后10个帖子。

相关问题