اذهب إلى المحتوى
  • 0

خطأ في Flutter عند إسناد قائمة لأخرى The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'

زهراء الربيع

السؤال

انا اتعلم flutterعندما اقوم بإضافة المتغيير يعطيني خطأ

The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'

كلما حاولت تغيير الList<dynamic>اليList<Widget>لااستطيع

وها هو كودى

import 'package:flutter/material.dart';



void main() {

  runApp(const EkhtbarApp());

}



class EkhtbarApp extends StatelessWidget {

  const EkhtbarApp({Key? key}) : super(key: key);



  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      home: Scaffold(

        backgroundColor: Colors.orange[100],

        appBar: AppBar(

          // ignore_for_file: prefer_const_constructors

          backgroundColor: Colors.brown[600],

          title: Text("اختر الإجابة الصحيحة"),

        ),

        body: Padding(

          padding: const EdgeInsets.all(20.0),

          child: Esst(),

        ),

      ),

    );

  }

}



class Esst extends StatefulWidget {

  const Esst({Key? key}) : super(key: key);



  @override

  _EsstState createState() => _EsstState();

}



class _EsstState extends State<Esst> {

  List fafa  = [

    Padding(

      padding: const EdgeInsets.all(3.0),

      child: Icon(

        Icons.thumb_up,

        color: Colors.lightGreen[400],

      ),

    ),

    Padding(

      padding: const EdgeInsets.all(3.0),

      child: Icon(

        Icons.thumb_down,

        color: Colors.red[600],

      ),

    ),

    Padding(

      padding: const EdgeInsets.all(3.0),

      child: Icon(

        Icons.thumb_up,

        color: Colors.lightGreen[400],

      ),

    ),

    Padding(

      padding: const EdgeInsets.all(3.0),

      child: Icon(

        Icons.thumb_down,

        color: Colors.red[600],

      ),

    ),

  ];

  @override

  Widget build(BuildContext context) {

    return Column(

      children: [

،،هنا هى المشكلة

        Row(children:fafa,

        ),

       

        Expanded(

          flex: 5,

          child: Column(

            children: [

              Image.asset("images/000.jpg"),

              SizedBox(

                height: 5.0,

              ),

              Text(

                "الشخصية في الصورة من كرتون المهند",

                textAlign: TextAlign.center,

                style: TextStyle(fontSize: 24.0),

              ),

            ],

          ),

        ),

        Expanded(

          child: Row(

            crossAxisAlignment: CrossAxisAlignment.stretch,

            children: [

              Padding(

                padding: const EdgeInsets.all(20.0),

                child: TextButton(

                  onPressed: () {

                    // Pop here with "صح"...

                  },

                  child: const Text(

                    'صح',

                    style: TextStyle(color: Colors.white),

                  ),

                  style: ButtonStyle(

                    backgroundColor:

                        MaterialStateProperty.all(Colors.lightGreen[400]),

                    fixedSize: MaterialStateProperty.all(Size.fromWidth(200)),

                  ),

                ),

              ),

              Padding(

                padding: const EdgeInsets.all(20.0),

                child: Expanded(

                  child: TextButton(

                    onPressed: () {

                      // Pop here with "خطأ"...

                    },

                    child: const Text(

                      'خطأ',

                      style: TextStyle(color: Colors.white),

                    ),

                    style: ButtonStyle(

                      backgroundColor:

                          MaterialStateProperty.all(Colors.red[600]),

                      fixedSize: MaterialStateProperty.all(Size.fromWidth(200)),

                    ),

                  ),

                ),

              ),

            ],

          ),

        ),

      ],

    );

  }

}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 3

تحتاج إلى تحديد نوع محتويات القائمة List، وذلك عبر تحديد نوع Widget كالتالي:

List<Widget> fafa  = [
// ^^^^^^^^^
    Padding(
      padding: const EdgeInsets.all(3.0),
      child: Icon(
        Icons.thumb_up,
        color: Colors.lightGreen[400],
      ),
    ),

    Padding(
      padding: const EdgeInsets.all(3.0),
      child: Icon(
        Icons.thumb_down,
        color: Colors.red[600],
      ),
    ),
  
    Padding(
      padding: const EdgeInsets.all(3.0),
      child: Icon(
        Icons.thumb_up,
        color: Colors.lightGreen[400],
      ),
    ),
  
    Padding(
      padding: const EdgeInsets.all(3.0),
      child: Icon(
        Icons.thumb_down,
        color: Colors.red[600],
      ),
    ),
  ];

يحدث هذا الأمر لأن المعامل children في الصف Row يحتاج إلى قائمة من نوع Widget بشكل إفتراضي، ويمكنك التأكد من ذلك من خلال وقوف بمؤشر الفأرة على كلمة children في محرر VS Code وسيظهر لك نوع هذا المعامل، كما في الصورة:

61785b4b22a3a_Screenshot2021-10-26214716.png.3a4f5d7aae47e7ed56a0804350363600.png

ملاحظة: إن لم تقم بتحديد نوع الكائنات في القائمة، فسيكون نوع القائمة <List<dynamic بشكل إفتراضي، وهذا بالطبع مخالف لما يستقبله المعامل children

يمكنك أيضًا أن تستخدم معامل النشر Spread Operator ( ... )، كما وضح المدرب وائل في إجابته.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 1

حاولي نشر القائمة:

Widget build(BuildContext context) {

    return Column(

      children: [

        Row(children: ...fafa,
                     ^^^^^^^^
        ),

أو

children: <Widget>[...fafa]

أو

children: <Widget>[
          for(var item in fafa ) Text(item)
      ],

مهما كان نوع القائمة التي أنشأناها، سنحولها إلى قائمة widgets

أرجو مشاركة الحل الذي يعمل.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...