不能将参数类型“int”分配给参数类型“String”

时间:2021-03-19 13:24:42

标签: flutter dart

我正在尝试找出这个错误。尽管我使用了 Rocket Guide — 第 3 部分,尽管我更改了一堆代码并更改了一些代码以满足我的需要。我在 id 中收到此错误:gunpla.id 让它真的很难修复。我试图找到它并尝试了一些其他适合的解决方案,但它们不起作用。如果有人可以帮助我并向我展示解决方案,我将不胜感激。这部分是从 firebase 实现 firestore。

Gunpla gunpla
package:gunpla_database/gunpla_details/gunpla_details_screen.dart

The argument type 'int' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)
import 'package:ant_icons/ant_icons.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gunpla_database/backend/backend.dart';
import 'package:cupertino_icons/cupertino_icons.dart';
import 'package:provider/provider.dart';

class GunplaDetailsScreen extends StatelessWidget {
  const GunplaDetailsScreen({
    Key key,
    @required this.gunpla,
  })  : assert(gunpla != null),
        super(key: key);

  final Gunpla gunpla;

  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;

    return Scaffold(
      appBar: AppBar(
        title: Text(gunpla.name),
        actions: [
          StreamBuilder<List<String>>(
              stream: context.read<Backend>().favoritedGunplas,
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }
                final favoritedGunplas = snapshot.data;
                final isGunplaFavorited = favoritedGunplas.contains(gunpla.id);
                return IconButton(
                  onPressed: () {
                    context.read<Backend>().setFavoritedGunpla(
                          id: gunpla.id,  <-- Error Is here
                          favorited: !isGunplaFavorited,
                        );
                  },
                  icon: isGunplaFavorited
                      ? const Icon(
                          AntIcons.heart,
                          color: Colors.redAccent,
                        )
                      : const Icon(AntIcons.heart_outline),
                );
              }),
        ],
      ),
      body: ListView(
        children: [
          if (gunpla.image.isNotEmpty) _ImageHeader(gunpla: gunpla),
          ListTile(
            title: Text(
              gunpla.name,
              style: textTheme.headline6,
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            child: Text(
              gunpla.series,
              style: textTheme.subtitle1,
            ),
          ),
          const Divider(),
          ListTile(
            leading: const Icon(AntIcons.column_width),
            title: Text('${gunpla.scale}'),
            subtitle: const Text('Available Scale'),
          ),
          const Divider(),
          ListTile(
            leading: const Icon(AntIcons.colum_height),
            title: Text('${gunpla.grade}'),
            subtitle: const Text('Available Grade'),
          ),
          const Divider(),
          ListTile(
            leading: const Icon(CupertinoIcons.star_fill),
            title: Text('${gunpla.exclusive}'),
            subtitle: const Text('Exclusive'),
          ),
          const Divider(),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: SizedBox(
              height: 56.0,
            ),
          ),
        ],
      ),
    );
  }
}

class _ImageHeader extends StatelessWidget {
  const _ImageHeader({
    Key key,
    @required this.gunpla,
  }) : super(key: key);

  final Gunpla gunpla;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 250,
      child: Hero(
        tag: 'hero-${gunpla.id}-image',
        child: Image.network(
          gunpla.image,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

显示的代码还不够,您可能需要添加高达模型类的代码。 但是,您很可能给这些文本小部件之一提供了一个整数。只需将 .toString() 添加到 Text 小部件内的 int 中即可。