Strapi+Flutter踩过的一些坑

今天根据这篇Build a To-Do App with Strapi GraphQL Plugin and Flutter试着用strapi+flutter组合做个todo的CRUD,记录一下原文的一些错误内容,问题主要集中在Flutter里面,前面strapi的安装和后端配置都很简单,下面正入主题:

1.依赖缺失

pubspec.yaml配置flutter的依赖中,原文写的是

dependencies:
  flutter:
    sdk: flutter
  intl:
  graphql_flutter: ^4.0.0-beta

这里少了个 http:

2.语法问题

文中大量类似于i d: todos\[index\]["id"] 这样的写法,这里有问题的是不该出现\,我在VSCODE中加入了flutter的语法插件,但是没有提示这个错误。

另外还有CreateTodo({Key key, this.refresh}) : super(key: key);这类代码如果报参数错误,可以通过Key? key或者required Key key来修复这个问题

参考:https://stackoverflow.com/questions/64560461/the-parameter-cant-have-a-value-of-null-because-of-its-type-in-dart

3.模拟器中的http代理问题

我不确定是否是和作者安装的Android Studio开发环境的版本有出入,原文作者特地重点提出模拟器使用的是代理地址,IP是10.0.2.2

The code above sets the link where the GraphQLClient will resolve documents. See that the link is http://10.0.2.2:1337/graphql, but why is that? Our Strapi backend runs on http://localhost:1337/graphql, not on http://10.0.2.2:1337/graphql. The answer is that we are running the Flutter app on an emulator.

而在实际部署过程中我发现用10.0.2.2是行不通的,得到的是连接超时的错误,而后来我将GraphQLConfig.dart文件中的IP改为127.0.0.1,程序可以正常运行,正确的写法

static HttpLink httpLink = HttpLink(
      'http://127.0.0.1:8080/graphql',
    );

Post Comment