You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:如何为展开列表项设置不同价格变量并解决类型错误

Fixing "The argument type 'String' can't be assigned to the parameter type 'int'" in Flutter ExpansionTile Price Setup

Let's fix this error and get your ExpansionTiles showing custom prices as intended!

What's Causing the Error?

Your MyTile class constructor is defined to take price (an int) as the first parameter, followed by title (a String), then optional children:

MyTile(this.price,this.title, [this.children = const <MyTile>[]]);

But when you create MyTile instances, you're passing the title string first (like 'Gin Cocktails') — that means you're trying to assign a string to the price variable (which expects an integer), hence the type mismatch error. On top of that, most of your child tiles don't have a price value passed at all.

Step-by-Step Fix

  1. Adjust Constructor Parameter Order (For Intuition)
    Swap the order of title and price in the MyTile constructor so it matches how you're actually writing your tile data. We'll also make price nullable since parent expansion tiles don't need to show a price:

    MyTile(this.title, {this.price, this.children = const <MyTile>[]});
    

    Using named parameters for price and children makes the code clearer and avoids order mix-ups.

  2. Assign Proper Integer Prices to Child Tiles
    Go through your listOfTiles and add integer price values to each child tile that needs to display a price. Parent tiles (the ones that expand) can omit the price parameter.

  3. Update the ListTile Subtitle Logic
    In the _buildTiles method, only show the subtitle if price is not null (so parent expansion tiles don't display an empty or 0 price).

Full Corrected Code

import 'package:flutter/material.dart';
void main() { runApp(new TileApp()); }

class TileApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Center(
            child: Text('ExpansionTile App'),
          ),
          actions: <Widget>[
            Icon(Icons.shopping_basket, size:35)
          ],
        ),
        body: new ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return new StuffInTiles(listOfTiles[index]);
          },
          itemCount: listOfTiles.length,
        ),
      ),
    );
  }
}

class StuffInTiles extends StatelessWidget {
  final MyTile myTile;
  StuffInTiles(this.myTile);

  @override
  Widget build(BuildContext context) {
    return _buildTiles(myTile);
  }

  Widget _buildTiles(MyTile t) {
    if (t.children.isEmpty) {
      return new ListTile(
        dense: true,
        enabled: true,
        isThreeLine: false,
        onLongPress: () => print("long press"),
        onTap: () => print("tap"),
        // Only show subtitle if price exists
        subtitle: t.price != null ? new Text("R${t.price.toString()}") : null,
        leading: new Text("Leading"),
        selected: true,
        trailing: GestureDetector(child: Icon(Icons.add, color: Colors.green)),
        title: new Text(t.title)
      );
    }
    return new ExpansionTile(
      key: new PageStorageKey<int>(3),
      title: new Text(t.title),
      children: t.children.map(_buildTiles).toList(),
    );
  }
}

class MyTile {
  String title;
  int? price; // Make price nullable
  List<MyTile> children;
  // Updated constructor with named parameters for clarity
  MyTile(this.title, {this.price, this.children = const <MyTile>[]});
}

List<MyTile> listOfTiles = <MyTile>[
  new MyTile(
    'Gin Cocktails',
    children: <MyTile>[
      new MyTile('Clover Club', price: 24),
      new MyTile('Tanqueray Sling', price: 28),
      new MyTile('1934 Cosmo', price: 30),
    ],
  ),
  new MyTile(
    'Beers & Ciders',
    children: <MyTile>[
      new MyTile('Heineken NRB', price: 18),
      new MyTile('Corona', price: 22),
    ],
  ),
  new MyTile(
    'Wine List',
    children: <MyTile>[
      new MyTile('Wine By The Glass', price: 25),
      new MyTile('White Wine', price: 32),
      new MyTile(
        'Red Wine',
        children: <MyTile>[
          new MyTile('Spier Signature Merlot', price: 35),
          new MyTile('Glenelly Glass Collection', price: 40),
          new MyTile('Nederburg Baronnen ', price: 38),
          new MyTile('Nero D’Avola ', price: 36),
          new MyTile('Reyneke Organic Cab Sav/Merlot ', price: 42),
        ],
      ),
    ],
  ),
];

Key Improvements

  • No more type mismatch errors because we're passing integer prices where expected
  • Parent expansion tiles don't show a price (cleaner UI)
  • Named parameters make the tile creation code easier to read and maintain
  • Subtitle now formats the price with the "R" prefix as you had originally

内容的提问来源于stack exchange,提问作者Lefentse Mphahlele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:52:08