رفتن به مطلب
انجمن اندروید ایران | آموزش برنامه نویسی اندروید و موبایل
  • android.png.1fab383bc8500cd93127cebc65b1dcab.png

پست های پیشنهاد شده

سلام..ارور زیر رو میتونید توضیح بدید و اینکه جدیدترین روش اتصال به سرور در فلاتر چیه...تو آپدیت فلاتر؟؟

 

E/flutter ( 3503): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String'

// ignore_for_file: unused_element, sized_box_for_whitespace, avoid_unnecessary_containers
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:nursing_eltiam/model/post.dart';

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

  @override
  _BuildBodyState createState() => _BuildBodyState();
}

class _BuildBodyState extends State<BuildBody> {
  List<Post> data = [];

  getAllPost() async {
    const url = "http://10.0.2.2:8000/api/v1/";
    http.Response response =
        await http.get(Uri.parse(url), headers: {'Accept': 'application/json'});
    setState(() {
      final jsonData = jsonDecode(utf8.decode(response.bodyBytes));
      for(var i in jsonData){
          var postItem = Post(i['id'],i['title'],i['category'],
              i['body'],i['favorite'] ,i['image']);
          data.add(postItem);
      }
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAllPost();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(5, 0, 5, 0),
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      child: ListView(
        children: [
          Container(
            margin:
                const EdgeInsets.only(top: 10, left: 5, right: 5, bottom: 10),
            height: 160,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              image: const DecorationImage(
                fit: BoxFit.fill,
                image: NetworkImage(
                    'https://s21.picofile.com/file/8446024768/nurse.jpg'),
              ),
            ),
          ),
          const SizedBox(
            height: 12,
          ),
          Container(
            height: 30,
            padding: const EdgeInsets.only(right: 10),
            child: const Text(
              'شاخه های پرستاری',
              style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.blueGrey,
                  fontFamily: 'Tanha'),
            ),
          ),
          _slide_one(),
          const SizedBox(
            height: 12,
          ),
          Container(
            margin: const EdgeInsets.all(5),
            height: 150,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              image: const DecorationImage(
                fit: BoxFit.fill,
                image: NetworkImage(
                    'https://s21.picofile.com/file/8446036542/Nursing.jpg'),
              ),
            ),
          ),
          const SizedBox(
            height: 12,
          ),
          Container(
            height: 30,
            padding: const EdgeInsets.only(right: 10),
            child: const Text(
              'پژوهش و مقالات',
              style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.blueGrey,
                  fontFamily: 'Tanha'),
            ),
          ),
          _slide_two(),
          const SizedBox(
            height: 12,
          ),
          Container(
            height: 30,
            padding: const EdgeInsets.only(right: 10),
            child: const Text(
              'اخبار پرستاری',
              style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.blueGrey,
                  fontFamily: 'Tanha'),
            ),
          ),
          _slide_three(),
          const SizedBox(
            height: 12,
          ),
          Container(
            margin: const EdgeInsets.all(5),
            height: 150,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              image: const DecorationImage(
                fit: BoxFit.fill,
                image: NetworkImage(
                    'https://s21.picofile.com/file/8446036850/Nurse1.jpg'),
              ),
            ),
          ),
          const SizedBox(
            height: 12,
          ),
          Container(
            height: 30,
            padding: const EdgeInsets.only(right: 10),
            child: const Text(
              'خدمات پرستاری',
              style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.blueGrey,
                  fontFamily: 'Tanha'),
            ),
          ),
          Container(
            height: 90,
            child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: 10,
                itemBuilder: (context, index) {
                  return Container(
                    width: 110,
                    child: Card(
                      elevation: 3,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                      ),
                      shadowColor: Colors.grey,
                      child: Center(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              child: Text(
                                'دسته بندی $index',
                                style: const TextStyle(fontSize: 12),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  );
                }),
          ),
          const SizedBox(
            height: 12,
          ),
          Container(
            margin: const EdgeInsets.all(5),
            height: 150,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              image: const DecorationImage(
                fit: BoxFit.fill,
                image: NetworkImage(
                    'https://s21.picofile.com/file/8446036950/nurse_staffing_.jpg'),
              ),
            ),
          ),
          const SizedBox(
            height: 12,
          ),
          Container(
            height: 30,
            padding: const EdgeInsets.only(right: 10),
            child: const Text(
              'دانستنی های پرستاری',
              style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.blueGrey,
                  fontFamily: 'Tanha'),
            ),
          ),
          _slide_four(),
        ],
      ),
    );
  }

  // ignore: non_constant_identifier_names
  Widget _slide_one() {
    return Container(
      height: 90,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: data.length,
          itemBuilder: (context, index) {
            var post = data[index];
            return Container(
              width: 110,
              child: Card(
                elevation: 3,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                shadowColor: Colors.grey,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          post.title,
                          style: const TextStyle(fontSize: 12),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            );
          }),
    );
  }

  // ignore: non_constant_identifier_names
  Widget _slide_two() {
    return Container(
      height: 90,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: 10,
          itemBuilder: (context, index) {
            return Container(
              width: 110,
              child: Card(
                elevation: 3,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                shadowColor: Colors.grey,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          'دسته بندی $index',
                          style: const TextStyle(fontSize: 12),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            );
          }),
    );
  }

  // ignore: non_constant_identifier_names
  Widget _slide_three() {
    return Container(
      height: 90,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: 10,
          itemBuilder: (context, index) {
            return Container(
              width: 110,
              child: Card(
                elevation: 3,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                shadowColor: Colors.grey,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          'دسته بندی $index',
                          style: const TextStyle(fontSize: 12),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            );
          }),
    );
  }

  // ignore: non_constant_identifier_names
  Widget _slide_four() {
    return Container(
      height: 90,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: 10,
          itemBuilder: (context, index) {
            return Container(
              width: 110,
              child: Card(
                elevation: 3,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                shadowColor: Colors.grey,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          'دسته بندی $index',
                          style: const TextStyle(fontSize: 12),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            );
          }),
    );
  }
}

 

لینک ارسال
به اشتراک گذاری در سایت های دیگر
  • 1 سال بعد...

 

به این شکل حلقه زدن هم اصلا پیشنهاد نمیشه

اول داخل کلاس مدل یک متد به نام fromJson بسازید که کار تبدیل جیسون به آبجکت عادی و انجام بده

var jsonResponse = json.decode(response.body);
  var mylist = jsonResponse.map((job) => new Post.fromJson(job)).toList();

 

لینک ارسال
به اشتراک گذاری در سایت های دیگر

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...