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

PlutoGridStateManager构造函数参数传入失败求助(v7.0.2)

PlutoGrid 7.0.2 中设置gridFocusMode与scrolling的正确方式

你遇到的问题是因为PlutoGrid 7.x版本对配置方式做了调整,gridFocusMode和scrolling不再是PlutoGridStateManager构造函数的直接参数,而是需要通过PlutoGridConfiguration来配置。另外你代码里写的gridfocusmode、linked都是未定义的变量,也需要修正。

正确实现步骤:

  • 先创建PlutoGridConfiguration实例,在其中配置gridFocusMode和scrolling
  • 将配置实例传入PlutoGridStateManager的构造函数
  • 确保使用正确的枚举值(GridFocusMode.row、PlutoGridScrolling.none)

修正后的代码:

class _HistoryPageState extends State<HistoryPage> {
  PlutoGridStateManager? stateManager;

  Future<void> readJson() async {
    // rootBundle.loadString: Load sample as Strings
    final String response =
        await rootBundle.loadString('assets/MyInfo.json');
    final List<dynamic> data = await json.decode(response)["items"];

    final List<PlutoColumn> columns = [
      PlutoColumn(title: 'No', field: 'No', type: PlutoColumnType.text()),
      PlutoColumn(title: 'Name', field: 'name', type: PlutoColumnType.text()),
    ];

    final List<PlutoRow> rows = data.map((item) {
      return PlutoRow(cells: {
        'No': PlutoCell(value: item['No']),
        'name': PlutoCell(value: item['name']),
      });
    }).toList();

    // 创建配置实例,设置需要的参数
    final configuration = PlutoGridConfiguration(
      gridFocusMode: GridFocusMode.row,
      scrolling: PlutoGridScrolling.none,
    );

    setState(() {
      // 将配置传入StateManager构造函数
      stateManager = PlutoGridStateManager(
        columns: columns,
        rows: rows,
        configuration: configuration,
      );
    });
  }
}

另外补充一个更推荐的用法:通常在使用PlutoGrid时,建议通过PlutoGrid组件的onLoaded回调获取stateManager,避免手动实例化带来的生命周期问题,示例如下:

PlutoGrid(
  columns: columns,
  rows: rows,
  configuration: PlutoGridConfiguration(
    gridFocusMode: GridFocusMode.row,
    scrolling: PlutoGridScrolling.none,
  ),
  onLoaded: (PlutoGridOnLoadedEvent event) {
    stateManager = event.stateManager;
  },
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 02:33:26