Posts Tagged ‘data provider’

In the course of migration from Flex3 project to Flex4 usually we faced common TypeError at runtime. These error are generally a type coercion failed error. The simple usecase of this is we are converting <mx:List> component to <s:List> component. Here we have an inline xml object for “imgList” and binding it to a <mx:List> component.

It was working fine but after changing the <mx:List> component into <s:List> we are faced runtime TypeError like this:

|TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@284a62a9 to mx.collections.IList.

Runtime error
<s:List labelField=”@name” dataProvider=”{ imgList.image }”/>

Works fine
<mx:List labelField=”@name” dataProvider=”{ imgList.image}”/>

The xml object of imagelist is as such:
var imgList:XML = ‘<imagelist>
<image name=”Image 1” source=”assets/image_01.jpg”/>

The <mx:List> component could take a few different objects as dataproviders, but the <s:List> spark List is typed only for taking IList type of object. The fact that it’s named “List”, XMLList turned out to be a top level Flash object that doesn’t implement the mx.collection.IList interface, which is required for a data source to be qualified as a DataProvider. The commonly used classes that implement IList are ArrayCollection, ArrayList (curiously) and XMLListCollection.

So the solution of this is to we need to typecast dataprovider data object into IList type.
dataProvider=”{new XMLListCollection(imageList.image)}”

<s:List labelField=”@name” dataProvider=”{new XMLListCollection(XMLList(imageList.image))}”/>

For more information on collection and data provider check this page :

P.S. – The code written in post are only for reference purpose.