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

Flutter API登录后调用商品接口报错Seller Not Logged In求助

问题排查与解决方法

核心问题根源

你使用的FlutterSession是Flutter端本地键值对存储工具,和后端接口识别登录态依赖的会话机制完全独立。绝大多数PHP后端(你当前使用的是OpenCart体系接口,路由规则符合OpenCart特征)的登录态是靠响应头Set-Cookie返回的Session ID识别,后续请求必须携带对应Cookie才能被后端识别为已登录状态,你当前的代码完全没有处理Cookie逻辑,所以请求商品接口时后端识别不到登录状态。

排查步骤

  • 先在Postman中按顺序调用登录接口、商品接口,确认Postman中可以正常拿到商品数据:如果Postman也返回未登录,说明是后端接口权限配置问题;如果Postman返回正常,就是前端请求未携带身份凭证的问题。
  • 检查登录接口的响应头,确认是否存在set-cookie字段,该字段的值就是后续请求需要携带的会话凭证。
  • 你当前代码中fetchSellerProduct里的FlutterSession().get('token')没有赋值、也没有传给接口,属于完全无效的代码,且你存在本地的是邮箱值,不是后端需要的身份凭证。

解决方法

方法1:使用Dio库自动管理Cookie(推荐)

Dio搭配Cookie管理插件可以自动保存登录接口返回的Cookie,后续请求自动携带,无需手动处理:

  1. 添加依赖到pubspec.yaml:
dependencies:
  dio: ^5.4.0
  dio_cookie_manager: ^3.1.1
  cookie_jar: ^4.0.8
  1. 全局初始化同一个Dio实例,所有接口都用该实例发起请求:
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';

// 全局复用同一个实例,保证Cookie统一管理
final dio = Dio();
final cookieJar = CookieJar();

void initDio() {
  dio.interceptors.add(CookieManager(cookieJar));
  dio.options.baseUrl = "https://www.nearxt.com";
  dio.options.connectTimeout = const Duration(seconds: 10);
  dio.options.receiveTimeout = const Duration(seconds: 10);
}
  1. 改造登录请求代码:
postMethod() async {
  Map mapeddate ={
    'email':_email.text,
    'password':_password.text,
  };
  final response= await dio.post("/index.php?route=api/userlogin", data: mapeddate);
  if(response.statusCode==200)
  {
    // Cookie已经自动保存,不需要再用FlutterSession存无效的邮箱值
    print("登录成功,会话已保存");
  }
  print(response.data);
}
  1. 改造商品接口请求代码:
Future fetchSellerProduct() async {
  // 同一个Dio实例会自动携带登录时保存的Cookie
  var response = await dio.post("/index.php?route=api/purpletree_multivendor/api/sellerproduct");
  if(response.statusCode==200)
  {
    print(response.statusCode);
  }
  setState(() {
    map = response.data;
    print(map);
  });
}

方法2:原生http库手动处理Cookie

如果不想更换http库,可以手动提取、携带Cookie:

  1. 登录时提取保存Cookie:
// 全局存储会话Cookie
String? sessionCookie;

postMethod() async {
  var api = Uri.parse("https://www.nearxt.com/index.php?route=api/userlogin");
  Map mapeddate ={
    'email':_email.text,
    'password':_password.text,
  };
  final response= await post(api,body: mapeddate);
  if(response.statusCode==200)
  {
    // 从响应头提取会话Cookie
    String? rawCookie = response.headers['set-cookie'];
    if (rawCookie != null) {
      sessionCookie = rawCookie.split(';')[0];
    }
    print("Session Created");
  }
  var res = json.decode(response.body);
  print(res);
}
  1. 商品请求时主动携带Cookie:
Future fetchSellerProduct() async {
  var url = await http.post(
    Uri.parse("https://www.nearxt.com/index.php?route=api/purpletree_multivendor/api/sellerproduct"),
    headers: {
      'Cookie': sessionCookie ?? '',
    }
  );
  if(url.statusCode==200)
  {
    print(url.statusCode);
  }
  setState(() {
    map = json.decode(url.body);
    print(map);
  });
}

特殊情况说明

如果后端接口要求用Token校验而非Cookie,需要登录接口返回后端生成的Token,你存在本地之后,后续请求放在请求头中传递即可,示例:

// 请求头加Token字段
headers: {
  'Authorization': 'Bearer 你存储的后端返回的Token',
}

内容的提问来源于stack exchange,提问作者Arpit Jai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:48:01